Laravel — P10: Blade with If and Switch Directives

Create IF and SWITCH Statements With Blade

Displaying data is one thing, but Blade has directives such as PHP’s if and switch statements. Blade directives are just shortcuts for implementing PHP code. You can even create your own Blade Directives, and we’ll look at that much later. For now, let’s focus on the built in ones: @if and @switch.

Read the previous article on Displaying Data in Blade Files.

@if Directive

As mentioned above, blade directives replicate actual PHP code. The regular PHP way of writing an if statement is:

<?php

if ($something == true) {
// Do something here
}
PHP also has the elseif and else statements that pair up with the if statement.
<?php

if ($something == "This Value") {
    // Do something here
} elseif ($something == "Other Value") {
    // Do something else
} else {
    // Do something if everything fails
}

Blade has directives for those as well: @elseif directive and @else directive. Let’s start with the @if directive.

What we’ll build is a page that displays the GT-R’s horsepower based on the year that’s entered.

Route::get('/car-if-directive/{year}', function($year) {
    return view('if-directive/gtr-if', [
        'year' => $year,
    ]);
});

GT-R Horsepower ranges as follows:

  • 1969–1973: 160hp
  • 1989–2002: 280hp
  • 2007–2009: 480hp
  • 2010: 485hp
  • 2011–2012: 530hp
  • 2013–2016: 545hp
  • 2017–2022: 565hp

Let’s write it out with our @if directive in our resources/views/if-directive/gtr-if.blade.php.

<body class="p-6">
The {{ $year }} GT-R has:

@if($year >= 1969 && $year <= 1973)
    160hp
@endif

</body>

That displays the first year. We do need to add an @endif directive to complete the if statement.

We can now just add more if statements to complete the full list.

<body class="p-6">
    The {{ $year }} GT-R has:
    
    @if($year >= 1969 && $year <= 1973)
        160hp
    @endif
    
    @if($year >= 1989 && $year <= 2002)
        280hp
    @endif
    
    @if($year >= 2007 && $year <= 2009)
        480hp
    @endif
    
    @if($year == 2010)
        485hp
    @endif
    
    @if($year >= 2011 && $year <= 2012)
        530hp
    @endif
    
    @if($year >= 2013 && $year <= 2016)
        545hp
    @endif
    
    @if($year >= 2017 && $year <= 2022)
        565hp
    @endif
</body>

If we visit our route and send a year, like http://localhost/car-if-directive/2019, we get the following result: The 2019 GT-R has: 565hp. Let’s make this cleaner. Too many @if and @endif directives. We’ll do this with the @elseif directive.

<body class="p-6">
    The {{ $year }} GT-R has:
    
    @if($year >= 1969 && $year <= 1973)
        160hp
    @elseif($year >= 1989 && $year <= 2002)
        280hp
    @elseif($year >= 2007 && $year <= 2009)
        480hp
    @elseif($year == 2010)
        485hp
    @elseif($year >= 2011 && $year <= 2012)
        530hp
    @elseif($year >= 2013 && $year <= 2016)
        545hp
    @elseif($year >= 2017 && $year <= 2022)
        565hp
    @endif
</body>

That’s much better. There’s only one thing that we’re missing, and that’s what happens when a user enters a year that’s outside of the ranges. We need the @else directive at the end.

<body class="p-6">
    The {{ $year }} GT-R has:
    
    @if($year >= 1969 && $year <= 1973)
        160hp
    @elseif($year >= 1989 && $year <= 2002)
        280hp
    @elseif($year >= 2007 && $year <= 2009)
        480hp
    @elseif($year == 2010)
        485hp
    @elseif($year >= 2011 && $year <= 2012)
        530hp
    @elseif($year >= 2013 && $year <= 2016)
        545hp
    @elseif($year >= 2017 && $year <= 2022)
        565hp
    @else
        No Information Found
    @endif
</body>

@switch Directive

Doesn’t this example look like it should be rolled up into a switch statement? I’m glad you thought so too. A typical switch statement looks like this:

<?php

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
        echo "i is outside of bounds";
}

Blade has directives for everything that we need to reconstruct our switch statement. It comes with @switch@case@break@default, and @endswitch directives. You’ll quickly see that each directive needs to be closed, which is why we have @endswitch. In PHP, we do that with curly braces.

@switch($i)
    @case(0)
        i equals 0
        @break
 
    @case(1)
        i equals 1
        @break

    @case(2)
        i equals 2
        @break
 
    @default
        Default case
@endswitch

To replicate our if/elseif/else example above is straightforward with Blade.

<body class="p-6">
The {{ $year }} GT-R has:

@switch($year)
    @case($year >= 1969 && $year <= 1973)
        160hp
        @break
    @case($year >= 1989 && $year <= 2002)
        280hp
        @break
    @case($year >= 2007 && $year <= 2009)
        480hp
        @break
    @case($year == 2010)
        485hp
        @break
    @case($year >= 2011 && $year <= 2012)
        530hp
        @break
    @case($year >= 2013 && $year <= 2016)
        545hp
        @break
    @case($year >= 2017 && $year <= 2022)
        565hp
        @break
    @default
        No Information Found
@endswitch

</body>
That’s it. Easy introduction into actual directives. If you’re a PHP developer, this is a no-brainer. If you’re a front-end developer with no PHP experience, you don’t have to learn PHP. The directives are self explanatory.

Laravel Series

Continue your Laravel Learning.

Laravel — P9- Displaying Data in Blade Files

Discussing Blade Directives

Laravel – P9: Displaying Data In Blade

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 — P10: Blade with If and Switch Directives

Create IF and SWITCH Statements With Blade

Laravel – P10: Blade With IF and SWITCH Directives

Displaying data is one thing, but Blade has directives such as PHP’s if and switch statements. Blade directives are just shortcuts for implementing PHP code.

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.

Leave a Reply