这是我在控制器中的getIndex()函数中的内容
public function getIndex() {
$categories = Category::all();
foreach ($categories as $category) {
$categories[$category->id] = $category->name;
}
......
}
因此,我希望从循环中获取所有类别名称。
但是,例如,如果我想通过在视图中执行此操作来获得结果
@foreach ($categories as $name)
<ul>
<li>{{var_dump($name)}}</li>
</ul>
@endforeach
结果就像
object(Category)#169(20){[“” fillable“:protected] => array(1){[0] => string(4)” name“} [” connection“:protected] => NULL [” table “:protected] => NULL [” primaryKey“:protected] => string(2)” id“ [” perPage“:protected] => int(15)[” incrementing“] => bool(true)[” timestamps “] => bool(true)[”属性“:受保护] => array(4){[”“ id”] => string(1)“ 1” [“ name”] => string(3)“ foo1” [“ created_at”] =>字符串(19)“ 2014-11-08 14:29:30” [“ updated_at”] =>字符串(19)“ 2014-11-08 14:29:30”} [“原始“:protected] => array(4){[”“ id”] => string(1)“ 1” [“ name”] => string(3)“ foo1” [“ created_at”] =>字符串(19)“ 2014-11-08 14:29:30” [“ updated_at”] =>字符串(19)“ 2014-11-08 14:29:30”} [“关系”:受保护] =>数组(0){} [“隐藏”:受保护] =>数组(0){} [“可见”:受保护] =>数组(0){} [“追加”:受保护] =>数组(0 ){} [“ guarded”:保护] => array(1){[0] => string(1)“ *”} [“ dates”:protected] => array(0){} [“ touches”: protected] => array(0){} [“ observables”:protected] => array(0){} [“ with”:protected] => array(0){} [“ morphClass”:protected] => NULL [“ exists”] => bool(true)}array(0){} [“ hidden”:protected] => array(0){} [“ visible”:protected] => array(0){} [“ appends:protected] => array(0){ } [“ guarded”:protected] => array(1){[0] => string(1)“ *”} [“ dates”:protected] => array(0){} [“ touches”:protected] => array(0){} [“ observables”:受保护] => array(0){} [“ with”:protected] => array(0){} [“ morphClass”:protected] => NULL [“存在“] => bool(true)}array(0){} [“ hidden”:protected] => array(0){} [“ visible”:protected] => array(0){} [“ appends:protected] => array(0){ } [“ guarded”:受保护] => array(1){[0] =>字符串(1)“ *”} [“ dates”:受保护] => array(0){} [“ touches”:protected] => array(0){} [“ observables”:受保护] => array(0){} [“ with”:protected] => array(0){} [“ morphClass”:protected] => NULL [“存在“] => bool(true)}array(0){} [“ observables”:protected] => array(0){} [“ with”:protected] => array(0){} [“ morphClass”:protected] => NULL [“ exists” ] => bool(true)}array(0){} [“ observables”:protected] => array(0){} [“ with”:protected] => array(0){} [“ morphClass”:protected] => NULL [“ exists” ] => bool(true)}
字符串(3)“ foo1”
第一个结果来自何处,我如何摆脱它?谢谢!
您可以尝试以下方法:
// Get an associative array
$categories = Category::lists('name', 'id');
然后将其传递给视图进行循环:
<ul>
@foreach ($categories as $id => $name)
<li>{{$name}}</li>
@endforeach
</ul>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句