Laravel — P11: Blade Loops

How Do You Loop With Blade Directives?

Blade directives have proven to be useful in the previous few articles, however, only now will they begin to shine. Loops can look quite messy with PHP when they’re embedded inside of HTML. Blade makes them look nice, almost like they belong there.

Read the previous article on IF and SWITCH Blade Directives.

Blade has your standard loops, like the forforeach and while loops, but it also has another one called forelse.

I’m going to assume that you know about the various different loops in PHP. If you don’t, take a look at my PHP articles on them.

PHP — P29: While Loops

PHP — P30: Do While Loops

PHP — P31: For Loop
PHP — P32: Foreach Loop

@for loop directive

The standard structure in PHP for a for loop is:

<?php

for ( $i = 0; $i < 10; $i++ ) {
    echo $i . "<br>";
}
With Blade, it’s just as simple.
@for ($i = 0; $i < 10; $i++)
    {{ $i }} <br>
@endfor

Since we’re not using curly braces, to end the loop body, we need to use the @endfor directive.

Let’s look at an actual example. We’ll create a route that passes a number to the our view. The view will then echo from 0 to the number that was passed.

<?php
Route::get('/loops/for/{value}', function($value) {
    return view('loops/for-loop', [
        'value' => $value,
    ]);
});
<body class="p-6">
    <h1>For Loop</h1>

    @for ($i = 0; $i <= $value; $i++)
        {{ $i }} <br>
    @endfor
</body>

Visiting http://0.0.0.0/loops/for/4 yields:

@foreach loop directive

Frequently, we need to pass array data. While we can use our @for loop directive, it’s cleaner to use our @foreach loop directive. For example,

$things_to_buy = [
    "meat shredder claws",
    "taco sleeping bag",
    "tortilla toaster",
    "the keyboard waffle iron"
];

// for loop
for ( $i = 0; $i < count($things_to_buy); $i++ ) {
    echo $things_to_buy[$i] . "<br>";
}

// foreach( $array as $array_value ) { loop body }
foreach ( $things_to_buy as $value ) {
    echo $value . "<br>";
}

It’s time to construct our route and view.

<?php
Route::get('/loops/foreach', function() {
    return view('loops/foreach-loop', [
        "things_to_buy" => [
            "meat shredder claws",
            "taco sleeping bag",
            "tortilla toaster",
            "the keyboard waffle iron"
        ],
    ]);
});
<h1>Foreach Loop</h1>

<ul>
    @foreach ($things_to_buy as $thing_to_buy)
        <li>{{ $thing_to_buy }}</li>
    @endforeach
</ul>

Pretty simple. Now, let’s visit our route: http://0.0.0.0/loops/foreach. You should see a page with the following content.

@forelse loop directive

How do we handle a circumstance when the view expects an array of data to be passed, but the array is empty? We could surround the @foreach directive with an @if directive, or we could simply use the @forelse loop directive.

When we use @forelse, and there’s nothing passed, we’ll need to write up a condition that handles that circumstance. You can use the @empty directive to handle it.

<?php

Route::get('/loops/forelse', function() {
    return view('loops/forelse-loop', [
        "things_to_buy" => [],
    ]);
});
<h1>Forelse Loop</h1>

<ul>
    @forelse ($things_to_buy as $thing_to_buy)
        <li>{{ $thing_to_buy }}</li>
    @empty
        <li>Nothing to Buy</li>
    @endforelse
</ul>

If we visit the route, we get:

What happens if we don’t pass the key in our route, like this:

<?php
Route::get('/loops/forelse', function() {
    return view('loops/forelse-loop');
});

We get an error since our view is expecting the things_to_buy key to be sent over.

@while loop directive

The standard structure in PHP for a for loop is:

<?php

$x = 0;

while( $x < 2 ) {
  echo $x;
  $x++;
}

With Blade, it’s just as simple.

@while ($x < 2)
    {{ $x++ }}
@endwhile

Since we’re not using curly braces, to end the loop body, we need to use the @endwhile directive.

Let’s look at an actual example. We’ll create a route that passes two number to a view: start and finish. The loop will echo each number from start to finish.

<?php
Route::get('/loops/while/{start}/{finish}', function($start, $finish) {
    return view('loops/while-loop', [
        'start' => $start,
        'finish' => $finish,
    ]);
});

<h1>While Loop</h1>
@while ($start <= $finish)
    {{ $start++ }} <br>
@endwhile

Visiting http://0.0.0.0/loops/while/1/4 yields:

Summary

We’ve gone through each of the loops available in Blade, but there’s still one additional concept related to loops that we haven’t covered, called the loop variable. We’ll look at the loop variable in the next article.

Laravel Series

Continue your Laravel Learning.

Laravel — P10: Blade with If and Switch Directives

Discussing Blade Directives

Laravel – P10: Blade With IF and SWITCH Directives

What we really wanted to achieve was introduce what Blade is, understand that there are Blade Directives. These directives make our lives easier and not PHP.

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

Leave a Reply