user3410847
user3410847

Reputation:

laravel using eloquent to populate select box and blade

I want to populate a select box with users from my database i need to get the id and name of the user.

I want to create a select box like the one below:

<select>
  <option value="$user->id">$user->name</option>
  <option value="$user->id">$user->name</option>
</select>

How can i do this?

Upvotes: 2

Views: 1388

Answers (2)

Asim
Asim

Reputation: 452

Controller:

View::share('users', User::all());

View:

<select name="user_id">
    @foreach($users as $user)
        <option value="{{$user->id}}">{{$user->name}}</option>
    @endforeach
</select>

Upvotes: 2

user0129e021939232
user0129e021939232

Reputation: 6355

try this:

controller

$users = User::all()->orderBy('name', 'asc')->lists('name','id');

view

{{ Form::select('user', $users , Input::old('users')) }}

Upvotes: 0

Related Questions