OunknownO OunknownO - 3 months ago 11
PHP Question

Dropdown menu with eloquent

I have a form and I want to give user ability to choose user in a drop down, but when I return the data it gives me an object, How can I make a drop down select for each user in array.

This is my code

view

{!! Form::select('users', array($users),null, ['placeholder' => 'Pick a user']) !!}


controller

$users = User::lists('name');
return view('view')->with('users', $users);


now it returns

Placeholder
["user1", "user2"]

Answer

You need to add ID to the list to make it work:

$users = User::pluck('name', 'id');

Also, use pluck() instead of lists() because lists() is depricated.

Comments