08 September 2016

How to Create Easy Dropdown List use Laravel Eloquent!

How to Create Easy Dropdown List use Laravel Eloquent! Hai guys, Many of you may know this already, but for those who don't this is really handy for building dropdowns. Suppose you want to show a categories dropdown whose information is pulled from an Eloquent model.
You can use the query builder's "lists" method (available for Eloquent as any query builder method) which returns an associative array you can pass to the Form::select method.


like this:
$categories = Category::lists('title', 'id');

note that the parameters you pass to lists correspond to the columns for the value and id, respectively
 in this case, "title" and "id"

you should pass this data to your view or somewhere in your view
{{ Form::select('category', $categories) }}
then build the dropdown inside it.
Easy guys :) .

No comments: