2016-09-15 61 views
0

我想在我的數據庫中存儲縣的數組。這裏是我的刀片:從選擇字段存儲數組

<select size="5" name="county[]" multiple class="form-control-2"> 
<option value="" selected="" disabled="">All Counties</option> 
@if (isset($counties)) 
    @foreach ($counties as $c) 
     <option value="{{ $c->name }}">{{ $c->name }}</option> 
    @endforeach 
@endif 

和我的控制器:

// Store the property Alert 
public function propertyAlert(PropertyAlertRequest $request) 
{ 
     $action = PropertySubscribe::create($request->all()); 
     $action = PropertySubscribe::create([ 
      $action->county = Input::get('county'), 
      ]); 
     $action->save(); 

    notify()->flash('Registered!', 'success', ['text' => 'You have now been registered.']); 
    return back(); 
} 

,我得到的錯誤是:

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array 

誰能幫助我瞭解我是什麼做錯了?我將它作爲數組發送,並且dd正在顯示值。我是否需要對數組項目進行foreach?

+1

爲什麼與依賴注入混個門面?你有注入的$ request,所以你不需要使用'Input ::'。使用'$ request-> input('county')'。 – Devon

回答

0

Input::get('county')是返回一個數組,因爲你的select multiple

試試這個:

foreach(Input::get('county') as $county) 
{ 
    PropertySubscribe::create([ 
     $action->county = $county, 
    ]); 
} 
+0

謝謝,我仍然得到preg_replace錯誤! –

+0

@PaoloResteghini以及PropertySubscribe :: create期望的是什麼?你在你的問題中傳遞了一個多維數組,這是一個單維數組。 – Devon

+0

我認爲這是一個錯字。 '$ action-> county => $ county,'但仍然沒有意義,左側應該是列的名稱。 – user3158900