语法:: parameterize()必须为数组类型 [英] Grammar::parameterize() must be of the type array
问题描述
我收到此错误
传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须为数组类型,给出字符串,
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given,
当我尝试使用选择表单在视图中添加array[]
时.但是,当我删除它时,我没有得到任何错误.我只是想在我的选择列表中输入多个值.我需要为此使用foreach
吗?
when I tried add array[]
in my View using select form. But when I removed it I didn't get any error. I'm just trying to input a multiple value in my select list. Do I need to use foreach
for this?
查看
<div class = "form-group {{ $errors->has('approver') ? ' has-error' : '' }}">
<label for = "approver" class = "control-label">Approver:</label>
<select name = "approver[]" multiple class = "form-control select2-multi">
@foreach ($approver as $list)
<option value = "{{ $list->id }}">{{ $list->username }}</option>
@endforeach
</select>
@if ($errors->has('approver'))
<span class = "help-block">{{ $errors->first('approver') }}</span>
@endif
</div>
控制器
public function getDocuments()
{
$approver = DB::table('users')->where('id', '!=', Auth::id())->get();
return view ('document.create')->with('approver', $approver);
}
public function postDocuments(Request $request)
{
$this->validate($request,
[
'title' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/|max:255',
'content' => 'required',
'category' => 'required',
'recipient' => 'required',
'approver' => 'required',
]);
$document = new Document();
$approve = new Approve();
$user = Auth::user();
//Request in the form
$document->title = $request->title;
$document->content = $request->content;
$document->category_id = $request->category;
$approve->approver_id = $request->approver;
$approve->save();
$document->save();
$document->sentToApprovers()->sync([$approve->id],false);
}
更新
我死掉并转储了$approver
变量,并给出了一个值数组.
I die and dump the $approver
variable and gives a array of value.
也死掉并丢弃$request
正如您在此处看到的那样,我在选择列表中输入了 4 和 5 的ID.
Also die and dump the $request
As you can see here I input the id of 4 and 5 in my select list.
推荐答案
好吧,您的问题是您试图将数组另存为单数,而需要在其上遍历批准者.
Ok, your issue is that you're trying to save an array as a singular value where you need to iterate over the approvers instead.
将控制器逻辑更改为此:
Change your controller logic around to this:
foreach ($request->approver as $approver) {
$approve = new Approve();
$approve->approver_id = $approver;
$approve->save();
$document->sentToApprovers()->sync([$approve->id],false);
}
这篇关于语法:: parameterize()必须为数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!