Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate select element

I want to populate a <select> element using the results from my categories table. In order to do that I use the following code.

$catlist = Categories::where('type','=',$content_type)
           ->get(array('id','name'))->toArray(); 

The result structure is an array of rows.

array
  0 => 
   array
      'id' => int 1
      'name' => string 'article' 
  1 => 
   array
     'id' => int 2
     'name' => string 'news' 

A foreach statement surely would solve my problem but I'm looking for a better solution.

like image 518
Yahia Reyhani Avatar asked Dec 16 '22 10:12

Yahia Reyhani


1 Answers

You could use the lists(string $column [, string $key ]) method for this, found under "Retrieving A List Of Column Values" in the documentation...

$catlist = Category::where('type', $content_type)->lists('name');
like image 69
Phill Sparks Avatar answered Dec 17 '22 22:12

Phill Sparks