OunknownO OunknownO - 5 months ago 22
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


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


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

now it returns

["user1", "user2"]


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.