Laravel:Blade foreach循环引导程序列 [英] Laravel: blade foreach looping bootstrap columns

查看:90
本文介绍了Laravel:Blade foreach循环引导程序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个foreach循环,并且其中包含带有引导程序列的html.

I have a foreach loop and inside that contains html with bootstrap columns.

@foreach($address as $add)
    <div class="col-md-6">
        Some data
    </div>
@endforeach

但是,引导程序在创建列之前需要行div,将其直接放入foreach循环将为每个col-md-6创建一个行div.我想知道如何将div扔进去,跳过仅在结束div标记中扔掉的下一个循环.然后重复该过程.

However, bootstrap requires the row div before creating columns, placing that straight in to the foreach loop would create a row div for each col-md-6. I want to know how I can throw in the row div, skip the next loop throwing in only the closing div tag. And then repeat that process.

示例输出,其中循环4次:

Example output where the loops 4 times:

<div class="row">
    <div class="col-md-6">
        Some data
    </div>
    <div class="col-md-6">
        Some data
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        Some data
    </div>
    <div class="col-md-6">
        Some data
    </div>
</div>

推荐答案

作为Alexey Mezenin回答的替代方法,您可以使用array_chunk. http://php.net/manual/en/function.array-chunk. php

As an alternative to Alexey Mezenin's answer you could use array_chunk instead. http://php.net/manual/en/function.array-chunk.php

@foreach(array_chunk($address, 2) as $chunk)
    <div class="row">
        @foreach($chunk as $add)
            <div class="col-md-6">
                Some data
            </div>
        @endforeach
    </div>
@endforeach

我个人认为上述内容更具可读性.

I personally find the the above a little more readable.

或者,如果$address是一个集合,则可以执行$address->chunk(2)而不是array_chunk($address, 2).

Alternatively, if $address is a collection you could do $address->chunk(2) instead of array_chunk($address, 2).

如果要更改所拥有的列数,只需将2更改为想要的列数即可.

If you want to change the amount of columns you have you would simply need to change the 2 to be however many columns you want.

这篇关于Laravel:Blade foreach循环引导程序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆