Search code examples
laravel-5eloquentformbuilderlaravelcollective

Laravel/formbuilder select option read out value with array


I'm writing a few cods around Laravel's form builder.

Here is my Controller:

public function create()
{
    $departments = Department::all('name');
    return view('door.project.create') ->with('departments',$departments);
}

On the create.blade.php, I get stuck. The selectbox's option would show out with JSON array, like: {"name": "Sale"}

And the select box codes would be down here:

<div class="form-group">
     {!! Form::Label('deparment_name', 'Department:') !!}
     {!! Form::select('deparment_name', $departments, null, ['mutiple' => 'multiple']) !!}
</div>

May I ask how do I fix it?


Solution

  • Instead of:

    $departments = Department::all('name');
    

    you should use:

    $departments = Department::pluck('name','id');
    

    to get in select valid list.