如何使用php在每三个元素后显示一个项目

jfgube3f  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(301)

我试着在每三分钟后显示一个项目 {{ $image->title }} ,
但是我使用的方法会在加载页面时产生一些延迟,即当我删除页面时 <div class='extra'> ,减少了200毫秒。
下面有没有最好的替代方法?

@foreach( $images as $index =>$image)  
 <div>
   {{ $image->title }}
 </div>
  @if( count($images) > 0 && $index != 0 && ($index % 3) == 0 )
    <div class="extra">
      Item to show after three Image Titles
    </div>
  @endif    
@endforeach
i2loujxw

i2loujxw1#

laravel有自己的blade$loop变量,用于帮助您查看此处的文档https://laravel.com/docs/8.x/blade#loops
在你的情况下,这样做更有意义

@foreach( $images as $index =>$image)  
 <div>
   {{ $image->title }}
 </div>
  @if( $loop->iteration % 3) // this can be $loop->index but im not sure what you need
    <div class="extra">
      Item to show after three Image Titles
    </div>
  @endif    
@endforeach

相关问题