When working with Blade templates in Laravel, developers often rely on loops to display lists of data such as product catalogs, user profiles, or blog posts. While a regular foreach loop works fine, Laravel goes one step further by introducing the $loop variable.
Mastering the $loop variable in Laravel Blade templates makes your code cleaner, more readable, and more powerful. This guide will show you how to use $loop effectively with real-world examples, ensuring smooth setup and integration in your Laravel projects.
Why Use $loop and Its Benefits
The $loop variable in Laravel Blade templates automatically becomes available when you use a foreach loop. It gives you extra information about the current loop iteration without writing additional logic like you use in for/while loop. Instead of managing counters or conditions manually, $loop helps you handle things like the current iteration number, whether the loop is on its first or last item, and even nesting levels when loops are inside one another.
With $loop, you save time, reduce code complexity, and create Blade templates that are easier to maintain and understand. You will understand this more with comparison example further.
Available Methods in $loop
Here is a complete list of properties you can use with $loop in Laravel Blade templates:

Comparison of Blade Foreach Loop vs for Loop
When building a Laravel application, we often need to display a list of items with extra details such as numbering, highlighting the first item, and marking the last one. Let’s try to achieve this using a regular PHP for loop first.
<ul>
@for ($i = 0; $i < count($events); $i++)
<li>
{{ $i + 1 }}. {{ $events[$i]->name }}
(Total Events: {{ count($events) }})
@if ($i === 0)
<strong>Featured Event</strong>
@endif
@if ($i === count($events) - 1)
<em>End of Event List</em>
@endif
</li>
@endfor
</ul>
This works, but notice how we had to manually manage the counter, calculate the last index, and call count($events) multiple times.
Now, Let’s see how easy it is to achieve the same result with the $loop variable in Laravel Blade templates:
<ul>
@foreach ($events as $event)
<li>
{{ $loop->iteration }}. {{ $event->name }}
(Total Events: {{ $loop->count }})
@if ($loop->first)
<strong>Featured Event</strong>
@endif
@if ($loop->last)
<em>End of Event List</em>
@endif
</li>
@endforeach
</ul>
Using the $loop variable in Laravel Blade templates is clearly simpler and cleaner. Instead of repeating count($events) and writing index-based conditions, $loop provides everything we need directly. This makes templates easier to read, reduces code repetition, and improves performance when setting up dynamic lists in Laravel applications.
Using $loop->parent in Nested Loops
When you work with nested loops in Laravel, the $loop->parent variable helps you access details from the parent loop.
For instance, imagine displaying a list of categories where each category contains some products.
@foreach ($categories as $category)
<h3>{{ $loop->iteration }}. {{ $category->name }}</h3>
<ul>
@foreach ($category->products as $product)
<li>
Category {{ $loop->parent->iteration }} - Product {{ $loop->iteration }}: {{ $product->title }}
</li>
@endforeach
</ul>
@endforeach
This use case shows how $loop->parent keeps track of relationships between parent and child loops, making it simpler to integrate hierarchical data in your Blade templates.
Conclusion
Mastering the $loop variable in Laravel Blade templates is a key skill for every developer who wants cleaner, smarter, and more maintainable code. With $loop, you no longer need to manually manage counters or write extra conditions which cost extra rending time and resource.
If you found mastering the $loop variable in Laravel Blade templates useful, you might also like exploring how to extend Blade further. Check out this post: Creating Custom Laravel Blade Directive
to learn how to build your own directives and make your Blade templates even more powerful.