Laravel:刀片 foreach 循环引导列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41650670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:07:24  来源:igfitidea点击:

Laravel: blade foreach looping bootstrap columns

phplaravelloopsforeachlaravel-5.3

提问by Thomas Charlesworth

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

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

@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.

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

Example output where the loops 4 times:

循环 4 次的示例输出:

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

回答by Rwd

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

作为 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 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 $addressis a collection you could do $address->chunk(2)instead of array_chunk($address, 2).

或者,如果$address是一个集合,你可以$address->chunk(2)代替array_chunk($address, 2).

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

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

回答by Isuru

You can use Laravel chunk in the blade template.

您可以在刀片模板中使用 Laravel 块。

@foreach($products->chunk(3) as $items)
<div class="row">
   @foreach($items as $item)
   <div class="col-md-4 portfolio-item">
      <a href="#">
      <img class="img-responsive" src="{{ 'uploads/'.$item->product_image_url }}" alt="">
      </a>
      <h3>
         <a href="/view-product-details/{{ $item->id }}">{{ $item->product_name }}</a>
      </h3>
      <p>{{ str_limit($item->product_description, 121) }}</p>
   </div>
   @endforeach
</div>
@endforeach

Copied from the blogpost.

复制自博文

回答by Alexey Mezenin

Use the $loopvariable:

使用$loop变量

<div class="row">
    @foreach($address as $add)
        <div class="col-md-6">
            Some data
        </div>
        @if ($loop->iteration % 2 == 0)
            </div>
            <div class="row">
        @endif
    @endforeach
</div>