将多个div添加到foreach循环-Laravel

时间:2019-06-25 09:41:27

标签: php laravel for-loop

我是Laravel的新手。我正在尝试通过多个div进行循环,并介于两者之间。我想在其中包装有多个列的div。

@foreach($products as $product)
<div>
   <div class="row">
       @foreach($products as $product)
        <div class="col-md-4">
          {{image}} 
        </div>

       @endforeach
   </div>
</div>
@endforeach

它没有打印预期的输出。我的预期输出应该是这样。

<div>
    <div class="row">
        <div class="col-md-4">image1</div>
        <div class="col-md-4">image2</div>
        <div class="col-md-4">image3</div>
     </div>
</div>

<div>
    <div class="row">
        <div class="col-md-4">image4</div>
        <div class="col-md-4">image5</div>
        <div class="col-md-4">image6</div>
    </div>
</div>

如何使用上述格式正确打印div?

3 个答案:

答案 0 :(得分:1)

尝试使用集合块:

@foreach($products->chunk(3) as $chunk)
   <div class="row">
       @foreach($chunk as $product)
        <div class="col-md-4">
          YOUR IMAGE HERE 
        </div>
       @endforeach
   </div>
@endforeach

答案 1 :(得分:0)

尝试一下?

<div class="row">
    @for($i = 1; $i <= len($products); $i++)
            <div class="col-md-4">
              Title: {{$products[$i-1]->title }}
            </div>  
      @if($i%3===0)
        </div>  <div class="row">
      @endif
    @endfor
 </div>

答案 2 :(得分:0)

@for($i = 0; $i < count($products); $i++)
<div>
   <div class="row">
       @foreach($products as $product)
        <div class="col-md-4">
          {{image}} 
        </div>

       @endforeach
   </div>
</div>
@endfor