Laravel Select Menu / Dropdown

时间:2015-07-31 19:54:21

标签: list laravel select populate

I am using the following code:

<select onchange="javascript:location.href = this.value;">

<option value="" selected>-- Show Genre --</option>

@foreach ($genres as $genre)
<option value="{{ URL::route('tracks.order', array('order' => 'name', 'by' => 'asc', 'genre' => $genre->name )) }}">{{ $genre->name }}</option>
@endforeach

</select>

The $genres is populated using the following code in my controller:

$genres     = Genres::lists('name', 'id');

I was wondering if I could use a code like below instead of the foreach? I think I should be able to, but I have no clue on how to change the code to work with my code above.

{{ Form::select('genre', array('default' => '-- Select Genre --') + $genres, '') }}

1 个答案:

答案 0 :(得分:1)

You may try something like this:

$genres = ['default' => '-- Select Genre --'] + Genres::lists('name', 'id');

In the view:

{{ Form::select('genre', $genres, Form::old('genre')) }}