7

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>

7 Answers 7

30

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)

Sign up to request clarification or add additional context in comments.

4 Comments

I did not know about the lists method until I stumbled upon this however I wanted to clarify something I discovered. Omitting the 2nd parameter does not then make the lists method default to the Model's key. This wasn't the case in 3.2.12 as well as the most recent version of 4. You'll always want to pass the key if you want to properly populate the drop down (with the value of the option becoming the value of the key).
You'll probably want to sort your list to... Category::orderBy('category')->lists('category', 'id');
Thank you for the solution, i was searcing for this everywhere on the net.
And now, with Laravel 5, do not forget to add ->all() after ->lists()
4

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)}}

5 Comments

Yep, that's the problem.. trying to find out how to do that.
Have you tried this out? In short, it doesn't work.. end up with a bunch of optgroup fields and such. I was able to get CLOSE using something alone these lines.. will edit question in a minute
Perhaps using ->to_array() isn't sufficient, try actually setting the array like the changes I made now.
Nope.. the problem is that the array we're sending to the Form is indexed when it can't be... so it's making optgroups for every index key. It needs to be like so: Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');
Just use $categories[$cat->id] = $cat->category; instead. And don't use Category::all() but Category::get(array('name', 'id')), to only fetch the needed columns. (Or just use my answer ;))
3

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>

Comments

1

How about this ?

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

Comments

1

Just tweeking bgallagh3r's code a little the following will work:

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)}}

Comments

1

For Laravel 5.3.x

In Controller

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

In View

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

Comments

-1

for laravel 5.2

in controller

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

in view

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.