将foreach循环中断到三个循环的极限

y3bcpkx1  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(310)

我在数据库中循环并为每5个div插入一个新div,如下所示:
如何为laravel中的每N个对象插入div
我如何添加一个中断,使其仅限制每5个div的三次新div?

<?php $counter = 1;?>

@if(count($images))

@foreach($images as $image)

    <div class="grid-item">
      <a href='{{route('specImage', $image->id)}}'>
    </div>  

    @if($counter % 5 == 0) 

     <div class="grid-item">             
       <div class="new-div">           
    </div>  

    @endif

<?php $counter++; ?>

@endforeach

@endif
dffbzjpn

dffbzjpn1#

这个应该适合你:

@if (count($images))
    @foreach ($images as $counter => $image)
        @php $counter++; @endphp
        <div class="grid-item">
            <a href='{{route('specImage', $image->id)}}'>
        </div>
        @if ($counter % 5 == 0 && $counter <= 15)
            <div class="grid-item">
                <div class="new-div">           
            </div>
        @endif
    @endforeach
  @endif

相关问题