填充选择元素 [英] Populate select element

查看:87
本文介绍了填充选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用我的类别表格中的结果填充< select> 元素。为了做到这一点,我使用以下代码。

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(); 

结果结构是一列数组。

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

A foreach 将解决我的问题,但我正在寻找一个更好的解决方案。

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

推荐答案

您可以使用 lists(string $ column [,string $ key]) 方法,在检索列值列表在文档...

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');

这篇关于填充选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆