Popular select with result plus a null element

Asked

Viewed 90 times

-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, the select using {{ Form::select() }} occurs perfectly. The Master method returns the teachers in alphabetical order and takes only the name and the id with the list, so far it is obvious.

But I’d like to add one option at the beginning of collection "Choose a teacher". I did so:

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

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

How can I fix this?

2 answers

0

You can create the first null field as follows:

{{ Form::select('name',['' => 'Selecione']+$teachers,'') }}

0

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

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

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

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

I also see no problem explicitly providing a disabled option for select, for example:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.