Laravel pluck Array to string conversion

Viewed 9324

I am create complaint For that while inserting department for their complaint I am using pluck() method to retrieve data from department table and display complaint in select dropdown as array but the problem is it is not working as it says

Array to string conversion (View: C:\xampp\htdocs\test\resources\views\complaint\create.blade.php)

ComplaintController

 $department = Department::pluck('name','id')->all();

    return view('complaint.create',compact('department'));

create.blade.php

<strong>Department : </strong>
{!! Form::select('dep_id',$department,null,['class'=>'form-control']) !!}

Please help!

3 Answers

If it is not working try this:-

$department = Department::select('id','name')->get();
return view('complaint.create')->with(compact('department'));

Now your view like this:-

<strong>Department : </strong>
<select class="form-control" name="any-name">
@foreach($department as $dept)
 <option value="{{$dept->id}}">{{$dept->name}}</option>
@endforeach

Hope it helps!

Try changing it to $department = Department::all()->pluck('name','id')->toArray();

{!! Form::select('dep_id',$department,old('dep_id'),['class'=>'form-control', 'placeholder'=>'Select Any name']) !!}
Related