填充选择元素

2024-01-03

我想填充一个<select>元素使用我的结果categories桌子。为此,我使用以下代码。

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

结果结构是行数组。

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

A foreach声明肯定会解决我的问题,但我正在寻找更好的解决方案。


你可以使用lists(string $column [, string $key ]) https://github.com/laravel/framework/blob/v4.2.0/src/Illuminate/Database/Query/Builder.php#L1426-L1455方法,可以在下面找到“检索列值列表” http://laravel.com/docs/4.2/queries#selects在文档中...

$catlist = Category::where('type', $content_type)->lists('name');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

填充选择元素 的相关文章

随机推荐