Php 使用数组在集合中循环-Laravel
我有一个收集,我已经从数据库中提取使用findMany方法在雄辩。 当我添加集合时,它看起来像附加的图像。然后我尝试(在我的刀片中)循环遍历数组中的每个项,以便能够打印实际记录中的每个值Php 使用数组在集合中循环-Laravel,php,laravel,loops,laravel-blade,Php,Laravel,Loops,Laravel Blade,我有一个收集,我已经从数据库中提取使用findMany方法在雄辩。 当我添加集合时,它看起来像附加的图像。然后我尝试(在我的刀片中)循环遍历数组中的每个项,以便能够打印实际记录中的每个值 $question = Question::whereIn('id', [23,25])->get(); 然后,在我的刀刃上,我试图做到: @foreach ($question as $row => $innerArray) @foreach ($innerArray as $inner
$question = Question::whereIn('id', [23,25])->get();
然后,在我的刀刃上,我试图做到:
@foreach ($question as $row => $innerArray)
@foreach ($innerArray as $innerRow => $value)
{{$value->question}} //I was expecting "What is your favourite food" here
@foreach ($value->option as $choice)
<li>{{$choice}}</li> //I was expecting the list of the options here
@endforeach
@endforeach
@endforeach
@foreach($row=>$innerArray)
@foreach($innerArray作为$innerRow=>$value)
{{$value->question}//我在这里期待“你最喜欢的食物是什么?”
@foreach($value->option as$choice)
{{$choice} //我正期待着这里的选项列表
@endforeach
@endforeach
@endforeach
我做错了什么?正确的语法是:
@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
@foreach($q问题)
{{$q->question}//我在这里期待“你最喜欢的食物是什么?”
@foreach($q->选项作为$choice)
{{$choice} //我正期待着这里的选项列表
@endforeach
@endforeach
所有由Eloquent返回的多结果集都是illumb\Database\Eloquent\Collection
对象的实例,包括通过get方法检索或通过关系访问的结果。Eloquent collection对象扩展了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
}
}
{{$value->question}}
不应该是{{$value['question']},因为它是数组而不是对象属性?对于@foreach($value->option as$choice)
和$value['options']
和's'也一样,谢谢。实际上我有这个,但它不起作用。所以我认为我错了,结果是别的什么东西打破了它。为了添加选项,我需要对其进行json_解码,因为这是一个以字符串形式存储的数组,所以必须有效地使用@foreach(json_decode($question->options)作为$option),因为这里有一个字符串,所以必须对其进行解码。然而,拉雷维尔有比这更优雅的东西。读一点关于