Laravel雄辩地将查询分解成数组 [英] Laravel Eloquent splitting query into array
问题描述
我有一个称为规范的表格,其中包含字段
id,device_id,name,detail
两个不同设备的两行将是
1,1,weight,300kg
2,2,weight,250kg
现在我需要显示的方式是一个单一的表格比较了这两个设备,基本上是这样的:
@foreach($ specification as $ specification)
< TR>
< td> {{$ specification-> name}}< / td>
< td> {{$ specification-> detailOne}}< / td>
< td> {{$ specification-> detailTwo}}< / td>
< / tr>
@endforeach
我有问题获得正确的查询或按顺序拆分查询能够像上面那样通过阵列。任何人都会把我放在正确的心态上?我是否查询这两个规范,并以某种方式将其转换为可以像上面那样使用的数组,还是还有其他一些我应该看的东西?
这应该适合你
@foreach($ specifications-> groupBy('name')as $ name => $ specificationGrouped)
< tr>
< td> {{$ name}}< / td>
@foreach($ specificationGrouped as $ specification)
< td> {{$ specification ['detail']}}({{$ specification ['device_id']}})< / td>
@endforeach
< / tr>
@endforeach
Bit of a bad title, not quite sure how to research/describe what I'm trying to do.
I have a table called specifications with the fields
id, device_id, name, detail
Two rows for two different devices would for example be
1, 1, weight, 300kg
2, 2, weight, 250kg
Now the way I need to display it is in a single table compare both devices, essentially like this:
@foreach($specifications as $specification)
<tr>
<td>{{ $specification->name }}</td>
<td>{{ $specification->detailOne }}</td>
<td>{{ $specification->detailTwo }}</td>
</tr>
@endforeach
I'm having issues getting the right query or split the query in order to be able to go through the array like above. Anyone mind putting me on the right mindset? Do I query both specs and somehow resort it into an array I can use as above, or is there something else I should be looking at?
This should work for you
@foreach($specifications->groupBy('name') as $name => $specificationGrouped)
<tr>
<td>{{ $name }}</td>
@foreach($specificationGrouped as $specification)
<td>{{ $specification['detail'] }} ({{ $specification['device_id'] }})</td>
@endforeach
</tr>
@endforeach
这篇关于Laravel雄辩地将查询分解成数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!