Switch in Laravel 5 - Blade

后端 未结 8 523
南旧
南旧 2021-02-02 05:10

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

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


        
相关标签:
8条回答
  • 2021-02-02 05:41

    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
    
    0 讨论(0)
  • 2021-02-02 05:41

    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;
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题