【发布时间】:2015-01-05 15:13:21
【问题描述】:
这是我在控制器中的 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) ["attributes":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" } ["original":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" } ["relations":protected]= > array(0) { } ["隐藏":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":protected]=> array(0) { } ["with":p rotected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) }
string(3) "foo1"
- 字符串(11)“foo2”
第一个结果来自哪里,我该如何摆脱它?谢谢!
【问题讨论】: