Css 如何为laravel中的每N个对象插入div

Css 如何为laravel中的每N个对象插入div,css,collections,laravel-5,Css,Collections,Laravel 5,我有一个foreach循环,需要每n个对象插入一个div @if(count($articles)) @foreach($articles as $article) @if(this is the nth object) <div class="row margin-b-2"> @endif <div class="col-sm-4"> <a

我有一个foreach循环,需要每n个对象插入一个div

 @if(count($articles))

    @foreach($articles as $article)

        @if(this is the nth object)
            <div class="row margin-b-2">
        @endif
            <div class="col-sm-4">
                <a style="background-color: {{ $article->category->color }}" href="{{ route('category', ['category_slug' => $article->category->slug])  }}">{{ $article->category->title }}</a>
            </div>

        @if(this is the nth object)
           </div>
        @endif
    @endforeach
 @endif
@if(计数($articles))
@foreach($articles作为$article)
@if(这是第n个对象)
@恩迪夫
@if(这是第n个对象)
@恩迪夫
@endforeach
@恩迪夫

这是我试图做的基本内容。

我将使用模除运算符。例如,如果您希望在每9篇文章之后使用它:

<?php $counter = 1; ?>

@if(count($articles))

    @foreach($articles as $article)

        @if($counter % 9 == 0)
             <div class="row margin-b-2">
        @endif
        <div class="col-sm-4">
            <a style="background-color: {{ $article->category->color }}" href="{{ route('category', ['category_slug' => $article->category->slug])  }}">{{ $article->category->title }}</a>
        </div>

        @if($counter % 9 == 0)
           </div>
        @endif
   <?php $counter++; ?>
   @endforeach
 @endif

@如果(计算($物品))
@foreach($articles作为$article)
@如果($counter%9==0)
@恩迪夫
@如果($counter%9==0)
@恩迪夫
@endforeach
@恩迪夫

工作原理:模除返回余数。当您处于偶数倍数时,余数始终等于0。

谢谢,这是我尝试使用的方法,但在blade中尝试初始化变量时遇到了困难。