Search code examples
phplaravellaravel-5laravelcollective

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"]

Solution

  • 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.