简体   繁体   中英

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?

Controller:

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

View:

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

try this:

controller

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

view

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM