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

对着背影说爱祢 提交于 2019-12-23 06:58:07

问题


I got this error

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

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?

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 $approver variable and gives a array of value.

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


回答1:


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



回答2:


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

User::insert($arrayData)

and i'm done.




回答3:


In my case i not mention the name in :-

$request->input();

so i just change with

$request->input("name");

then its works




回答4:


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


来源:https://stackoverflow.com/questions/38957486/grammarparameterize-must-be-of-the-type-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!