If you work with Laravel Blade templates, one feature that can make your views much cleaner is the built-in $loop variable inside the @foreach directive

Many developers manually create counters or extra conditions inside loops, while Laravel already provides a powerful helper for this.

Here’s a simple example:

In this example, $loop->iteration gives you the current loop number starting from 1.

But the $loop variable can do much more:

  • $loop->iteration : Current iteration number (starts from 1)
  • $loop->index : Current index (starts from 0)
  • $loop->remaining : Remaining items in the loop
  • $loop->count : Total number of items
  • $loop->first : True if this is the first iteration
  • $loop->last : True if this is the last iteration
  • $loop->even : True if iteration number is even
  • $loop->odd : True if iteration number is odd

This is especially useful when building:

  • Tables
  • Lists
  • Timelines
  • Numbered sections
  • Dynamic UI components

Using Laravel’s $loop variable helps you write cleaner Blade templates, avoid unnecessary PHP logic inside views, and improve readability for future maintenance.

Small Laravel features like this are often overlooked, but they can significantly improve your development experience

What’s your favorite Blade feature in Laravel? 👇