Laravel — P12: Blade Foreach Loop Variable

A Powerful Blade Tool For Loops

An additional benefit to using the Blade syntax for your foreach loop is that you get access to the loop variable. The loop variable gives you access to various information such as how many iterations are left in the loop. If you haven’t read on Blade Loops, be sure to do so first.

https://medium.com/geekculture/laravel-p11-loops-fc8e11f61998

What is part of the $loop variable?

  • $loop->index the index of the current loop iteration (starts at 0).
  • $loop->iteration the current loop iteration (starts at 1).
  • $loop->remaining the iterations remaining in the loop.
  • $loop->count the total number of items in the array being iterated.
  • $loop->first whether this is the first iteration through the loop.
  • $loop->last whether this is the last iteration through 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.

To illustrate what each of these do, let’s pass an array of data and loop through it. First, the route.

<?php
Route::get('/loops/foreach-loop-variable', function() {
    return view('loops/foreach-loop-variable', [
        "cars_to_buy" => [
            "1999 Nissan Skyline GT-R",
            "1989 Nissan 240sx",
            "2022 Porsche 911 GT3",
            "1994 Toyota Supra 2JZGTE"
        ],
    ]);
});
We’ll normally want to loop through this array of data.
<h1>Foreach Loop Variable</h1>

    @foreach ($cars_to_buy as $car_to_buy)
        <ul>
            <li>Car to Buy: {{ $car_to_buy }}</li>
            <li>Loop Index: {{ $loop->index }}</li>
            <li>Current Iteration: {{ $loop->iteration }}</li>
            <li>Remaining Iterations: {{ $loop->remaining }}</li>
            <li>Total Iterations: {{ $loop->count }}</li>
            <li>Is this the First iteration: {{ $loop->first }}</li>
            <li>Is this the Last iteration: {{ $loop->last }}</li>
            <li>Is this an Even iteration: {{ $loop->even }}</li>
            <li>Is this an Odd iteration: {{ $loop->odd }}</li>
            <li>Loop Depth: {{ $loop->depth }}</li>
            <li>Loop Parent: {{ $loop->parent }}</li>
        </ul>
    @endforeach

</body>

Each iteration will provide us with the knowledge that we need. The results that we get are as follows. I’ve labeled them for better readability:

Foreach Loop Variable

- Car to Buy: 1999 Nissan Skyline GT-R
- Loop Index: 0
- Current Iteration: 1
- Remaining Iterations: 3
- Total Loop Count: 4
- Is this the First iteration: 1
- Is this the Last iteration:
- Is this an even iteration:
- Is this an odd iteration: 1
- Loop Depth: 1
- Loop Parent:

- Car to Buy: 1989 Nissan 240sx
- Loop Index: 1
- Current Iteration: 2
- Remaining Iterations: 2
- Total Loop Count: 4
- Is this the First iteration:
- Is this the Last iteration:
- Is this an even iteration: 1
- Is this an odd iteration:
- Loop Depth: 1
- Loop Parent:

- Car to Buy: 2022 Porsche 911 GT3
- Loop Index: 2
- Current Iteration: 3
- Remaining Iterations: 1
- Total Loop Count: 4
- Is this the First iteration:
- Is this the Last iteration:
- Is this an even iteration:
- Is this an odd iteration: 1
- Loop Depth: 1
- Loop Parent:

- Car to Buy: 1994 Toyota Supra 2JZGTE
- Loop Index: 3
- Current Iteration: 4
- Remaining Iterations: 0
- Total Loop Count: 4
- Is this the First iteration:
- Is this the Last iteration: 1
- Is this an even iteration: 1
- Is this an odd iteration:
- Loop Depth: 1
- Loop Parent:

Nothing complex about it with the exception of maybe the depth and parent variables. Let’s provide an example with what that might look like. You guessed it, nested arrays.

<?php
Route::get('/loops/foreach-loop-variable-nested', function() {
    return view('loops/foreach-loop-variable-nested', [
        "cars" => [
            "1999 Nissan Skyline GT-R" => [
                "specs" => [1000, "Manual"]
            ],
            "1989 Nissan 240sx" => [
                "specs" => [500, "Manual"]
            ],
            "2022 Porsche 911 GT3" => [
                "specs" => [650, "Manual"]
            ],
            "1994 Toyota Supra 2JZGTE" => [
                "specs" => [1000, "Manual"]
            ],
        ],
    ]);
});

This one’s a little tricky, so let’s walk through it. We’re passing the cars array. Each of the cars has an array containing specs. Each specs array contains the horsepower and transmission type. It’s always going to be Manual in case you didn’t know.

To loop through this, we need to loop through each of the cars and then through each of the specs. Nested foreach loops.

<h1>Foreach Loop Variable</h1>

@foreach($cars as $car => $specs)
    <ul>
        <li>{{ $car }}</li>
        <li>Outer Loop Index: {{ $loop->index }}</li>
        <li>Outer Loop Depth: {{ $loop->depth }}</li>
        <li>Outer Loop Parent: {{ $loop->parent }}</li>

        @foreach($specs as $spec)
            <li>Horsepower: {{ $spec[0] }}</li>
            <li>Transmission: {{ $spec[1] }}</li>
            <li>Inner Loop Depth: {{ $loop->depth }}</li>
            <li>Inner Loop Index: {{ $loop->index }}</li>
            <li>Outer Loop (Parent) Index: {{ $loop->parent->index }}</li>
        @endforeach
    </ul>
@endforeach
  • For the outer loop, we want to get both the key and the other array (specs). We need the key to print out the car that we’re on.
  • We print out the Outer Loop Index, the current depth, and the parent. This is a bit weird for the parent, since we won’t ever actually use the parent this way. We’ll see shortly why we need to the parent variable.
  • The depth for the outer array is always 1 since we’re in the outermost loop.
  • We proceed to go into the nested array for the first car, 1999 Nissan Skyline GT-R. We need to loop through the array that it possesses in order to extract the details for it. We really don’t need to, but we are for this example.
  • Once inside of the nested loop, we can print out the horsepower and the transmission type that we passed.
  • We also have access to the $loop variable here. This loop variable is for this inner loop.
  • When we check the loop depth, we notice it’s at 2.
  • Since it has only one thing we’re looping through, specs, the index will always be 0. We accessed the other elements with square brackets. This is what I meant by we don’t need to actually do a loop for the inner loop.
  • This is where the parent comes into play. If we needed to know which iteration the parent is currently on, we can use $loop->parent->index. You’ll notice that it matches the outer loop index each time.
Foreach Loop Variable

- 1999 Nissan Skyline GT-R
- Outer Loop Index: 0
- Outer Loop Depth: 1
- Outer Loop Parent:
- Horsepower: 1000
- Transmission: Manual
- Inner Loop Depth: 2
- Inner Loop Index: 0
- Outer Loop (Parent) Index: 0

- 1989 Nissan 240sx
- Outer Loop Index: 1
- Outer Loop Depth: 1
- Outer Loop Parent:
- Horsepower: 500
- Transmission: Manual
- Inner Loop Depth: 2
- Inner Loop Index: 0
- Outer Loop (Parent) Index: 1

- 2022 Porsche 911 GT3
- Outer Loop Index: 2
- Outer Loop Depth: 1
- Outer Loop Parent:
- Horsepower: 650
- Transmission: Manual
- Inner Loop Depth: 2
- Inner Loop Index: 0
- Outer Loop (Parent) Index: 2

- 1994 Toyota Supra 2JZGTE
- Outer Loop Index: 3
- Outer Loop Depth: 1
- Outer Loop Parent:
- Horsepower: 1000
- Transmission: Manual
- Inner Loop Depth: 2
- Inner Loop Index: 0
- Outer Loop (Parent) Index: 3

I hope that this helps explain the loop variable within Blade for you. See you next time when we go over a couple more Blade directives.

Laravel Series

Continue your Laravel Learning.

Laravel — P11: Blade Loops

How Do You Loop With Blade Directives?

Laravel – P11: Blade Loops

Loops can look quite messy with PHP when they’re embedded inside of HTML. Blade makes them look nice, almost like they belong there.

Laravel — P12: Blade Foreach Loop Variable

A Powerful Blade Tool For Loops

Laravel – P12: Blade Loop Variable

An additional benefit to using the Blade syntax for your foreach loop is that you get access to the loop variable, which gives you access to various information

Laravel — P13: Including Subviews in Blade

Breaking Apart the Views

Laravel – P13: Including Subviews In Blade

Splitting apart the code is something that makes it easier to edit. How do you combine the files later? By including subviews into your Blade files.

Leave a Reply