【发布时间】:2015-10-07 02:15:13
【问题描述】:
这是我尝试做的:
$q = Question::where('id',$id -> id)->get();
$q[] = $q->push([ 'test' => true]);
dd($q);
这将输出:
Collection {#220 ▼
#items: array:3 [▼
0 => Question {#225 ▶}
1 => array:1 [▼
"test" => true
]
2 => null
]
}
所以'test' => true 将作为一个新键追加,但我想将它插入Question 以便以后我可以像这样使用foreach $q -> test 访问它
这就是我想要访问项目的方式:
@foreach($q as $qq)
{{ $qq->test }}
@endforeach
【问题讨论】:
-
我不明白 1 件事:问题::where('id',$id -> id)->get();为什么您不使用主键获取元素: Question::find($id); ?
-
当行的集合总是唯一的时候,这很有趣(:
-
如果我使用
find是否可以添加新的随机属性?到现有对象 -
当然可以,但是你会失去它的 ORM 功能。
-
好的,我该怎么做?
标签: php arrays collections eloquent laravel-5