Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to show an item after every three elements using PHP

I am trying to show an item after every three {{ $image->title }},
but my method used creates a bit delay in loading the page, i.e. when I remove <div class='extra'>, there is reduction of 200ms.
Is there any best alternate method for below?

@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
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Laravel has its own Blade $loop variable that is meant to help you with this check out the docs here https://laravel.com/docs/8.x/blade#loops

In your case something like this makes more sense

 @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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!