我试图在我的数据库中存储县数组。这是我的刀片:
<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成功地显示了值。我需要对数组项进行查询吗?
Input::get('county')
返回一个数组,因为您的select multiple
试试这个:
foreach(Input::get('county') as $county)
{
PropertySubscribe::create([
$action->county = $county,
]);
}