从选择字段存储数组

时间:2016-09-15 14:38:21

标签: php laravel multiple-select

我正在尝试在我的数据库中存储一系列县。这是我的刀片:

<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成功显示值。我是否需要对数组项进行预测?

1 个答案:

答案 0 :(得分:0)

Input::get('county')正在返回一个数组,因为您的select multiple

试试这个:

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