Reputation:
I want to to use foreach loop in select form. But I dont figure out how can I do that.
I searched about it in here, in other questions and found lists method. But when I tried it retuns me "Call to undefined method App\Category::lists()" error.
{{ Form::select('categories',
"foreach loop" for bringing categories
) }}
Any advice ?
Upvotes: 2
Views: 1334
Reputation: 11083
As discribed in the Laravel Recipes :
If you use this :
{{ Form::select('age', ['Under 18', '19 to 30', 'Over 30']) }}
You will get this output :
<select name="age">
<option value="0">Under 18</option>
<option value="1">19 to 30</option>
<option value="2">Over 30</option>
</select>
So in your case you can use it like this :
{{ Form::select('categories', $categories->pluck('name')) }}
If you want to addthe id of the category as value of the option you can do it like this :
In the controller :
$categories = [''=>''] + Category::lists('name', 'id')->all();
return view('back.create')->withCategories($categories);
And in the view :
{{ Form::select('categories', $categories) }}
Upvotes: 3