Laravel Advanced: Know the sneaky $loop.

Blade provides directives like @foreach, @while, @for, and @forelse for working with PHP's loop. Did you know... A handy $loop variable...

Karan Datwani
Karan Datwani
Share:

Blade provides directives like @foreach, @while, @for, and @forelse for working with PHP's loop.

Did you know... A handy $loop variable is available within these directives, which tells about the current loop iteration? In this article, we'll explore $loop and loop directives.😎

Go deeper than @foreach with $loop

The @foreach directive iterates over arrays and objects, looping the data and render HTML for each item. Pretty simple:

@foreach($users as $user)
    <p>{{ $user->name }}</p>
@endforeach

But, within the @foreach loop, you have access to $loop variable, offering information like:

  • $loop->index: The current iteration index (0-based).
  • $loop->iteration: The current iteration number (1-based).
  • $loop->first: Telling if it's the first iteration.
  • $loop->last: Telling if it's the last iteration.
  • $loop->count: Total number of items in the loop.
  • $loop->remaining: Iterations remaining in the loop.
  • $loop->even: Whether this is an even iteration through the loop.
  • $loop->odd: Whether this is an odd iteration through the loop.
  • $loop->depth: The nesting level of the current loop.
  • $loop->parent: When in a nested loop, the parent's loop variable.

Pretty useful, right? Here is how you can use it:

@foreach($users as $user)
    <p>{{ $user->name }}</p>
    @if ($loop->last)
        <p>This is the last user.</p>
    @endif
@endforeach

Bonus: @forelse

The @forelse directive is similar to @foreach but includes a condition to handle empty arrays or collections.

@forelse($tasks as $task)
    <p>{{ $task->name }}</p>
@empty
    <p>No tasks found.</p>
@endforelse

In addition to the loop properties mentioned earlier, $loop also provides a property specific to @forelse:

  • $loop->empty: Boolean indicating if the loop is empty.

Second bonus: @for and @while

  • The @for directive executes a block of code for a specified number of iterations.
  • The @while directive executes a block of code as long as a condition is true.
@for ($i = 0; $i < 5; $i++)
    <p>{{ $i }}</p>
@endfor

@while (true)
    <p>I'm looping forever.</p>
@endwhile

Conclusion

By using the $loop variable, you gain access to useful loop information that is helpful to conditionally render content or apply styling.

All of the above have been previously shared on our Twitter, one by one. Follow us on Twitter; You'll ❤️ it.

You can also check the first article of the series, which is on the Top 5 Scheduler Functions you might not know about. Keep exploring, and keep coding with ease using Laravel. Until next time, happy looping! 🚀

Want to receive more articles like this?

Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.

Reactions & Comments

What do you think about this?

Latest Articles

Wondering what our community has been up to?