Laravel Blade 循环

Laravel Blade Loop

我正在使用 ceil() 方法来计算它应该创建多少 <div class="item"> 块。每个.item只能有4个img.

我在弄清楚如何仅允许 4 img 然后通过循环创建下一个 .item 块时遇到问题。

    @foreach(range(1, ceil($product->photos->count()/4)) as $section)
    <div class="item">
        <div class="row">
            @foreach($product->photos as $photo)
                <img src="{{$photo->photo_url}}" alt="" data-target="#carousel" data-slide-to="{{$loop->index}}" class="galleryItem">
            @endforeach
        </div>
    </div>
    @endforeach

你可以像这样在foreach中使用chunk方法

  @foreach($posts->chunk(2) as $tempPosts)
  <div class = "row">
   @foreach($tempPosts as $post)
   /* your  code*/
   @endforeach
  </div>
  @endforeach

使用chunk():

@foreach($product->photos->chunk(4) as $photos)
    <div class="item">
        <div class="row">
            @foreach($photos as $photo)
                <img src="{{$photo->photo_url}}" alt="" data-target="#carousel" class="galleryItem">
            @endforeach
        </div>
    </div>
@endforeach