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>
NightMICU
source share