Laravel:在具有不同大小的12列Bootstrap网格上循环遍历结果

huangapple go评论49阅读模式
英文:

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?

Laravel:在具有不同大小的12列Bootstrap网格上循环遍历结果

答案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 =&gt; $product){
	$product-&gt;col = $cols[$key % count($cols)];
}

Blade:

@foreach($products as $product)
&lt;div class=&quot;col-{{ $product-&gt;col }}&quot;&gt;&lt;/div&gt;
@endforeach

huangapple
  • 本文由 发表于 2023年2月14日 19:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定