For loop with iteration in Blade
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Iteration in Blade: Beyond Simple `foreach`
As a senior developer working within the Laravel ecosystem, you often find yourself transitioning between the imperative world of PHP logic and the declarative world of Blade templating. A common point of confusion arises when trying to replicate complex PHP control structures like `for` loops directly within a Blade file.
The short answer is: **Blade is not designed to handle heavy procedural iteration; it is designed for presentation.** The actual looping, data preparation, and conditional logic should almost always be handled in your Controller or Eloquent Model *before* the data is passed to the view.
However, understanding how Blade provides iteration helpers like `$loop->index` and `$loop->remaining` is crucial for mastering dynamic display. Let’s dive into why we use these variables and how they relate to iterating over collections in Laravel.
## The Philosophy of Blade Iteration
In traditional procedural PHP, you might execute a `for` loop to build an array structure:
```php
// Controller/Logic Layer (Where the work happens)
$result = [];
for ($i = 0; $i < 3; $i++) {
$result[$i] = ['id' => $i + 1, 'name' => 'Item ' . ($i + 1)];
}
// Pass $result to the view
```
When we move this logic into a Laravel application, we leverage powerful PHP features and Laravel’s Eloquent capabilities. Instead of manually creating a structure in a loop, we let the database or the Collection handle the iteration. This shift is fundamental to writing maintainable code, especially when dealing with data retrieval from the database, as seen with tools like those provided by [laravelcompany.com](https://laravelcompany.com).
## Using `$loop->index` and `$loop->remaining` in Blade
Blade excels at iterating over arrays or collections using the `foreach` directive. When you use `foreach`, Blade automatically makes special variables available within the loop context, most notably `$loop`. This object provides metadata about the current iteration.
### Understanding the Variables
1. **`$loop->index`**: This variable holds the current index (starting from 0) of the current item in the collection.
2. **`$loop->remaining`**: This variable holds the number of remaining items to be iterated over.
These variables are immensely useful when you need to perform actions based on the position of an element, such as adding numbering or slicing data.
### Practical Example: Iterating Over a Collection
Let's assume you have passed a collection of products from your controller to the view.
**Controller Logic (Preparing the Data):**
In your controller, you would load the data and pass it to the view:
```php
// Example Controller Snippet
$products = [
['id' => 1, 'name' => 'Laptop'],
['id' => 2, 'name' => 'Mouse'],
['id' => 3, 'name' => 'Keyboard']
];
return view('products.index', ['products' => $products]);
```
**Blade View (Iterating using `$loop`):**
Now, in your Blade file, you can iterate over this `$products` array and use the loop variables:
```blade
Product List
-
@foreach ($products as $product)
- Index: {{ $loop->index }} | Name: {{ $product['name'] }} (Remaining: {{ $loop->remaining }}) @endforeach