【问题标题】:Laravel Blade LoopLaravel 刀片循环
【发布时间】:2017-09-26 07:42:07
【问题描述】:

我正在使用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

【问题讨论】:

    标签: php laravel laravel-blade


    【解决方案1】:

    使用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
    

    【讨论】:

    • $product 是集合,而不是数组。
    • 哦,那就用chunk()吧。更新了答案。
    【解决方案2】:

    你可以在foreach中使用chunk方法

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

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 2017-11-13
      • 2016-06-29
      • 2017-05-29
      • 2015-12-01
      • 2016-03-08
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多