我有一个包含多个数据的对象:
$products = Products::whereIn('id', $input['product_id'])->where('customer_id', Auth::user()->customers_id)->get();
$products 的 dd 输出如下所示:
Collection {#287 ▼
#items: array:12 [▼
0 => Products {#288 ▶}
1 => Products {#289 ▶}
2 => Products {#290 ▶}
3 => Products {#291 ▶}
4 => Products {#292 ▶}
5 => Products {#293 ▶}
6 => Products {#294 ▶}
7 => Products {#295 ▶}
8 => Products {#296 ▶}
9 => Products {#297 ▶}
10 => Products {#298 ▶}
11 => Products {#299 ▶}
]
}
我将对象返回到我的 Blade 并尝试在 3 列上输出数据,如下所示:
@foreach($products->chunk(3) as $chunk)
<div class="row" style="width: 100%">
@foreach($chunk as $product)
<div style="width: 33%">{{ $product->name }}<br></div>
@endforeach
</div>
@endforeach
但这只是给了我它们之间的数据
喜欢:
A
B
C
D
...
但我需要它在 3 列上
喜欢:
A E I
B F J
C G K
D H L
感谢您的帮助!抱歉我的英语不好..
我在以下位置找到了 chunk 方法:
https://laravel.com/docs/5.1/collections
最佳答案
尝试为div's
添加float:left;
,它应该会产生您想要的结果:
@foreach($products as $chunk)
<div class="row" style="width: 100%">
@foreach($chunk as $product)
<div style="width: 33%; float:left">{{ $product->name }}<br></div>
@endforeach
</div>
@endforeach
希望有帮助 =)
关于php - 将一个对象切成 3 block 并在 Blade 中查看它们 - Laravel - chunk 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39160918/