Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate drop-down list input with values from DB table

Tags:

php

laravel

I am trying to generate a drop-down list with values from a MySQL table using Laravel. The table is simple, two columns - id and category.

The following will retrieve all of the records (categories) but returns an object rather than an array, which is what I need for the drop-down code -

$categories = Category::all();

The code for the drop-down is:

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

Ideas?

UPDATE

bgallagh3r suggested using a foreach loop to convert each category to an array. Their code got me close but generated a bunch of funky nested optgrouptags. I was able to get it down to just one optgroup but that is one too many..

$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 end up with this HTML:

    <select name="categories">
    <optgroup label="0">
    <option value="1">Department News</option>
    </optgroup>
    <optgroup label="1">
    <option value="2">General</option>
    </optgroup>
   ...
    </select>
like image 967
NightMICU Avatar asked Jan 08 '13 18:01

NightMICU


People also ask

Which type of input can be used to create drop down?

The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).


3 Answers

You can try the 'lists' function:

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

(Only for Laravel 3) Or even leave out the 'id' parameter, as it will default to the Model key, see http://laravel.com/api/source-class-Laravel.Database.Query.html#596

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

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

like image 139
Barryvdh Avatar answered Oct 10 '22 12:10

Barryvdh


Depending on if you are using 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)}}
like image 22
bgallagh3r Avatar answered Oct 10 '22 12:10

bgallagh3r


In Laravel 4, you can easily try like 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 view just add {{ Form::select('category', $category) }}. The result will be what you want, like the following

<select name="category">
        <option value="category_id">category_name</option>
</select>
like image 33
Set Kyar Wa Lar Avatar answered Oct 10 '22 12:10

Set Kyar Wa Lar