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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With