Search code examples
phplaravelpluck

Laravel pluck Array to string conversion


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!


Solution

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