英文:
Laravel: Loop through results on a 12 column bootstrap grid with different sizes
问题
在产品的foreach循环中,我想以不均匀的列尺寸显示每个元素,总共有12列,就像下面的截图中所示。我该如何在Laravel中实现这个目标?
英文:
Inside a foreach loop of products, I want to display each of the elements in unequal/uneven column sizes that make up a total of 12 just like what we have in the screenshot below. How can I achieve this in Laravel?
答案1
得分: 1
你可以在控制器中为每个产品分配col
:
$cols = [3, 9, 3, 6, 3, 6, 6];
foreach ($products as $key => $product) {
$product->col = $cols[$key % count($cols)];
}
Blade 模板:
@foreach ($products as $product)
<div class="col-{{ $product->col }}"></div>
@endforeach
英文:
You can assign the col
to each product in controller:
$cols = [3, 9, 3, 6, 3, 6, 6];
foreach($products as $key => $product){
$product->col = $cols[$key % count($cols)];
}
Blade:
@foreach($products as $product)
<div class="col-{{ $product->col }}"></div>
@endforeach
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论