Set selection item

I want to populate the <select> element using the results from the categories table. For this, I use the following code.

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

The result structure is an array of strings.

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

A foreach expression will probably solve my problem, but I'm looking for a better solution.

+4
source share
2 answers

You can use the lists(string $column [, string $key ]) method lists(string $column [, string $key ]) found in "Retrieving a List of Column Values" in the documentation ...

 $catlist = Category::where('type', $content_type)->lists('name'); 
+12
source

try adding a static function to your category model

 public static function genlist() { $catlist = []; foreach (self::all() as $cat) { $catlist[$cat->id] = $cat->name; } return $catlist; } 

View

 {{ Form::select('cat_id', Category::genlist()) }} 

it works for me, and hope will work for you too.

0
source

Source: https://habr.com/ru/post/1481944/


All Articles