Laravel 5.1 - 嵌套循环 - 刀片模板

时间:2015-09-05 23:56:38

标签: php loops foreach laravel-5 blade

我在查看

上有这个
..
...
<div class="section group">
    @foreach ($product as $item)
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">{!! $item->name !!}</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ {!! $item->price !!} </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>         
    @endforeach
</div>
...
..

这是控制器

..
...
public function index(){
    $product = product::paginate(8);
    return view('shop.index', compact('product'));
}
...
..

我想显示数据我将控制器分为两部分,因此输出将如下:

<div class="section group">
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 01</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 01 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 02</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 02 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 03</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 03 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 04</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 04 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
</div>

<div class="section group">
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 05</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 05 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 06</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 06 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 07</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 07 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
    <div class="grid_1_of_4 images_1_of_4">
        <h4><a href="#">item 08</a></h4>
        <div class="price-details">
            <div class="price-number">
                <p><span class="price">$ price 08 </span></p>
            </div>
            <div class="add-cart">                              
                <h4><a href="#">More Info</a></h4>
            </div>
            <div class="clear"></div>
        </div>                           
    </div>
</div>

我认为它应该使用嵌套循环,但我不知道如何在laravel中编写它。

任何人都知道应该怎么做?

2 个答案:

答案 0 :(得分:4)

http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Collection.html#method_chunk

我喜欢chunk()函数。比array_chunk更具可读性和可重用性

@foreach ($product->chunk(4) as $chunkedProducts)
<div class="section group">
@foreach ($chunkedProducts as $item)
<div class="grid_1_of_4 images_1_of_4">
    <h4><a href="#">{!! $item->name !!}</a></h4>
    <div class="price-details">
        <div class="price-number">
            <p><span class="price">$ {!! $item->price !!} </span></p>
        </div>
        <div class="add-cart">                              
            <h4><a href="#">More Info</a></h4>
        </div>
        <div class="clear"></div>
    </div>                           
</div>         
@endforeach
</div>
@endforeach

答案 1 :(得分:0)

This question正在解决一个非常类似的问题,请看一下。

引用:

  

您可以在$ product-&gt; all()上使用array_chunk将其拆分为4个组,然后创建一个嵌套的@foreach。

在这种情况下,它可能看起来像这样(未经测试!):

@foreach (array_chunk($product->all(), 4) as $productSection)
    <div class="section group">
        @foreach ($productSection as $item)
            <div class="grid_1_of_4 images_1_of_4">
                <h4><a href="#">{!! $item->name !!}</a></h4>
                <div class="price-details">
                    <div class="price-number">
                        <p><span class="price">$ {!! $item->price !!} </span></p>
                    </div>
                    <div class="add-cart">                              
                        <h4><a href="#">More Info</a></h4>
                    </div>
                    <div class="clear"></div>
                </div>                           
            </div>
        @endforeach
    </div>
@endforeach