【问题标题】:Store array from select field从选择字段存储数组
【发布时间】:2016-09-15 14:38:20
【问题描述】:

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

<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 吗?

【问题讨论】:

  • 为什么要将外观与依赖注入混合在一起?你已经注入了 $request,所以你不需要使用Input::。使用$request-&gt;input('county')

标签: php laravel multiple-select


【解决方案1】:

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

试试这个:

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

【讨论】:

  • @PaoloResteghini 那么 PropertySubscribe::create 期望什么?您在问题中传递了一个多维数组,这是一个单维数组。
  • 我认为这是一个错字。 $action-&gt;county =&gt; $county, 但这仍然没有意义,左边应该是列名。
  • @PaoloResteghini 看看你的$fillable property 模型,如果你使用的是..::create([ ...]), you dont need ->save()`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 2013-12-12
  • 1970-01-01
  • 2016-04-13
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多