Laravel:刀片 foreach 循环引导列 [英] Laravel: blade foreach looping bootstrap columns

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

问题描述

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

@foreach($address as $add)<div class="col-md-6">一些数据

@endforeach

但是,bootstrap 在创建列之前需要行 div,将其直接放入 foreach 循环将为每个 col-md-6 创建一个行 div.我想知道如何放入行 div,跳过仅放入结束 div 标签的下一个循环.然后重复这个过程.

循环 4 次的示例输出:

<div class="col-md-6">一些数据

<div class="col-md-6">一些数据

<div class="row"><div class="col-md-6">一些数据

<div class="col-md-6">一些数据

解决方案

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

@foreach(array_chunk($address, 2) as $chunk)<div class="row">@foreach($chunk 作为 $add)<div class="col-md-6">一些数据

@endforeach

@endforeach

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

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

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

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

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.

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>

解决方案

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.

Alternatively, if $address is a collection you could do $address->chunk(2) instead of array_chunk($address, 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:刀片 foreach 循环引导列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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