Popular select with result plus a null element

-1

I have a method that invokes the view as follows:

return view('auth.register',[
    'teachers' => $this->user->mentors()->lists('name','id'),
]);

And in the view, select using {{ Form::select() }} occurs perfectly. The mentors method returns the teachers alphabetically and takes only name and id with list , so far it's obvious.

But I'd like to add option at the beginning of collection "Choose a teacher". I did it this way:

collect(['' => 'Escolha um professor'])->merge($this->user->mentors()->lists('name','id'));

But doing so, the value of id is replaced by index , as if it were an array.

How can I fix this?

    
asked by anonymous 23.05.2016 / 20:49

2 answers

0

You can create the first null field as follows:

{{ Form::select('name',['' => 'Selecione']+$teachers,'') }}
    
23.05.2016 / 21:31
0

You can simply add the placeholder attribute in your html field, for example:

{!! Form::select('teacher', $teachers, null, ['placeholder' => 'Escolha um professor']) !!}

Or, if the escolha um professor option has a value other than the default, null for example, you can continue adding a new item at the beginning of the list, like this:

$teachers = array_replace(
    [null => 'Escolha um professor'],
    $this->user->mentors()->lists('name','id')->toArray()
);

I also see no problem in explicitly making a disabled option available to the select, for example:

<select>
    <option value="" disabled selected>Escolha um professor</option>
    <option value="Professor Eduardo">Eduardo</option>
    ...
</select>
    
17.06.2016 / 18:56