php Grammar::parameterize() 必须是数组类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38957486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:46:26  来源:igfitidea点击:

Grammar::parameterize() must be of the type array

phplaravellaravel-5.2

提问by Francisunoxx

I got this error

我收到这个错误

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given,

传递给 Illuminate\Database\Grammar::parameterize() 的参数 1 必须是数组类型,字符串给定,

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 foreachfor this?

当我尝试array[]使用选择表单添加我的视图时。但是当我删除它时,我没有收到任何错误。我只是想在我的选择列表中输入多个值。我需要为此使用foreach吗?

View

看法

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

Controller

控制器

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);

}

Update

更新

I die and dump the $approvervariable and gives a array of value.

我死了并转储了$approver变量并给出了一个值数组。

SC

标准委员会

Also die and dump the $requestAs you can see here I input the id of 4and 5in my select list.

也死并转储$request正如您在此处看到的那样,我在选择列表中输入了45的 ID 。

SC

标准委员会

采纳答案by Jared Eitnier

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);
}

回答by sh6210

In my case i wanted to insert bulk data, therefore i got the error. Then i used the

就我而言,我想插入批量数据,因此出现错误。然后我用

User::insert($arrayData)

User::insert($arrayData)

and i'm done.

我已经完成了。

回答by Adam

I got this error because I overwrote the constructor in a model. If you do so, make sure to pass the arguments array:

我收到此错误是因为我覆盖了模型中的构造函数。如果这样做,请确保传递参数数组:

  public function __construct(array $attributes = [], $something_else)
  {
    parent::__construct($attributes);

    //...
    $this->something_else = $something_else;
  }

回答by Krishna Kumar Jangid

In my case i not mention the name in :-

就我而言,我没有提到以下名称:-

$request->input();

so i just change with

所以我只是改变

$request->input("name");

then its works

那么它的作品