Creating a drop-down list with values ​​from the DB table

I am trying to create a dropdown list with values ​​from a MySQL table using Laravel. The table is simple, two columns are id and category .

All entries (categories) are listed below, but it returns an object, not an array, which I need for the dropdown code -

 $categories = Category::all(); 

Dropdown Code:

 {{ Form::select('category', $categories, $post->category_id) }} 

Ideas?

UPDATE

bgallagh3r suggested using a foreach loop to convert each category into an array. Their code closed me, but created a bunch of funky optgroup nested tags. I was able to bring it to one optgroup , but this is too much.

 $categories = Category::all(); foreach ($categories as $cat) { $category = $cat->to_array(); $id = $category['id']; $value = $category['category']; $cats[] = array($id => $value); } 

And then in the form:

 {{ Form::select('categories', $categories)}} 

I get this HTML code:

  <select name="categories"> <optgroup label="0"> <option value="1">Department News</option> </optgroup> <optgroup label="1"> <option value="2">General</option> </optgroup> ... </select> 
+7
source share
7 answers

You can try the Lists feature:

$categories = Category::lists('category', 'id');

(Only for Laravel 3) Or even not specify the parameter 'id', since it will be the default for the model key, see http://laravel.com/api/source-class-Laravel.Database.Query.html#596

$categories = Category::lists('category');

(For Laravel 4, you need the second parameter, see http://laravel.com/api/source-class-Illuminate.Database.Query.Builder.html#1034-1063 )

+30
source

Depending on whether you use Eloquent for your models, Category will return an array of objects, you need to convert the objects to arrays before passing them to the Form class.

 foreach (Category::all() as $cat) { $categories[] = array($cat->id => $cat->category); } {{ Form::select('categories', $categories)}} 
+4
source

In Laravel 4 you can easily try the following

 //Query category and pass it to the view that you want to show your category $category = Category::lists('category_name', 'category_id'); 

In your opinion, just add {{ Form::select('category', $category) }} . The result will be what you want, for example, as follows

 <select name="category"> <option value="category_id">category_name</option> </select> 
+3
source

How about this?

 foreach (Category::all() as $cat) { $categories[$cat->id] = $cat->category; } {{ Form::select('categories', $categories)}} 
+1
source

Just tweeking bgallagh3r code will work a bit:

 foreach (Category::all() as $cat) { $categories[$cat->id] = $cat->category); } {{ Form::select('categories', $categories)}} 

... although I prefer (in L4):

 foreach (Category::select('id', 'category')->orderBy('id','asc')->get() as $cat) { $categories[$cat->id] = $cat->category; } {{ Form::select('categories', $categories)}} 
+1
source

For Laravel 5.3.x

In the controller

 $categories = Category::pluck('name', 'id'); 

In view

 {{ Form::select('categories', $categories)}} 
+1
source

for laravel 5.2

in the controller

 $mechanics = User::where('role_id','3')->lists('name', 'id'); 

in sight

 {{ Form::select('mechanic_id', [''=>'please select'] + $mechanics->toArray(), $order->mechanic_id , array('id' =>'material-select2','class' => 'form-control')) }} 
0
source

All Articles