Switch in Laravel 5 - Blade

138,976

Solution 1

Updated 2020 Answer

Since Laravel 5.5 the @switch is built into the Blade. Use it as shown below.

@switch($login_error)
    @case(1)
        <span> `E-mail` input is empty!</span>
        @break

    @case(2)
        <span>`Password` input is empty!</span>
        @break

    @default
        <span>Something went wrong, please try again</span>
@endswitch

Older Answer

Unfortunately Laravel Blade does not have switch statement. You can use Laravel if else approach or use use plain PHP switch. You can use plain PHP in blade templates like in any other PHP application. Starting from Laravel 5.2 and up use @php statement.

Option 1:

@if ($login_error == 1)
    `E-mail` input is empty!
@elseif ($login_error == 2)
    `Password` input is empty!
@endif

Solution 2

You can just add these code in AppServiceProvider class boot method.

Blade::extend(function($value, $compiler){
        $value = preg_replace('/(\s*)@switch\((.*)\)(?=\s)/', '$1<?php switch($2):', $value);
        $value = preg_replace('/(\s*)@endswitch(?=\s)/', '$1endswitch; ?>', $value);
        $value = preg_replace('/(\s*)@case\((.*)\)(?=\s)/', '$1case $2: ?>', $value);
        $value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value);
        $value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value);
        return $value;
    });

then you can use as:

@switch( $item )
    @case( condition_1 )
        // do something
    @breakswitch
    @case( condition_2 )
        // do something else
    @breakswitch
    @default
        // do default behaviour
    @breakswitch
@endswitch

Enjoy It~

Solution 3

IN LARAVEL 5.2 AND UP:

Write your usual code between the opening and closing PHP statements.

@php
switch (x) {
    case 1:
        //code to be executed
        break;
    default:
        //code to be executed
}
@endphp

Solution 4

This is now built in Laravel 5.5 https://laravel.com/docs/5.5/blade#switch-statements

Solution 5

In Laravel 5.1, this works in a Blade:

<?php
    switch( $machine->disposal ) {
        case 'DISPO': echo 'Send to Property Disposition'; break;
        case 'UNIT':  echo 'Send to Unit'; break;
        case 'CASCADE': echo 'Cascade the machine'; break;
        case 'TBD':   echo 'To Be Determined (TBD)'; break;
    }
?>
Share:
138,976
ventaquil
Author by

ventaquil

Polish young student IT. I'm learning web languages and more. If you want to contact with me send me private message.

Updated on July 08, 2022

Comments

  • ventaquil
    ventaquil over 1 year

    How can I use switch in blade templates? When I used:

    @switch($login_error)
        @case(1)
            `E-mail` input is empty!
            @break
        @case(2)
            `Password` input is empty!
            @break
    @endswitch
    

    in result I see this text as plaintext. I prefer to use switch in few piece of code because it's more clean for me than when I using if.

    But if it's not possible just write it.

  • CashIsClay
    CashIsClay over 8 years
    To be fair, switch statements can certainly belong in the view. For example, if you're setting CSS classes based on data ranges in a table, you wouldn't want to embed display logic in the controller.
  • andres.gtz
    andres.gtz about 8 years
    This doesn't work. parse error, expecting &quot;endswitch (T_ENDSWITCH)&quot;&#039; or &quot;case (T_CASE)&quot;&#039; or &quot;default (T_DEFAULT)`
  • Germey
    Germey about 8 years
    @mkmnstr, Try to replace ` ' ` to ` " ` in preg_replace method?
  • magallanes
    magallanes almost 8 years
    "Business logic is not meant for views,". Business logic is for the visual layer, the logic layer and the persistence layer. Its tedious and redundant but a quality code does that.
  • Jonathan
    Jonathan over 7 years
    Anyone upvoted since these comments? Does this work?
  • Alexander Kim
    Alexander Kim over 7 years
    This @php syntax is very useful in L5.2 >
  • Kyle Challis
    Kyle Challis over 6 years
    Using Laravel 5.4, does not work for me. FatalThrowableError Class 'App\Providers\Blade' not found
  • Michael Villeneuve
    Michael Villeneuve over 6 years
    It does work on 5.5. I would just be curious on how to get it to work with phpstorm autocompletion (or any IDE) @Germey ?
  • Adam Moore
    Adam Moore over 6 years
    As per @baders answer, this is now supported natively in 5.5: laravel.com/docs/5.5/blade#switch-statements
  • Marjeta
    Marjeta about 6 years
    @KyleChallis put \ in front of Blade, or put use Blade on top of your file.