2 回答
TA贡献1831条经验 获得超9个赞
Eloquent 返回的所有多结果集都是该Illuminate\Database\Eloquent\Collection
对象的实例,包括通过 get 方法检索或通过关系访问的结果。Eloquent 集合对象扩展了 Laravel基础集合,因此它自然继承了数十种用于流畅地处理底层 Eloquent 模型数组的方法。
所有集合也用作迭代器,允许您像简单的 PHP 数组一样循环遍历它们:
$questions = Question::whereIn('id', [23, 25, ..])->get();
foreach ($questions as $question) {
echo $question->title; // What is your favourite food
echo $question->name; // food
foreach ($question->options as $option) {
echo $option; // Pizza
}
}
TA贡献1864条经验 获得超6个赞
正确的语法是这样的:
@foreach ($question as $q)
{{ $q->question }} //I was expecting "What is your favourite food" here
@foreach ($q->options as $choice)
<li>{{$choice}}</li> //I was expecting the list of the options here
@endforeach
@endforeach
- 2 回答
- 0 关注
- 173 浏览
添加回答
举报