Francisunoxx Francisunoxx - 3 months ago 71
PHP Question

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

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.

SC

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

SC

Answer

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