Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch in Laravel 5 - Blade

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.

like image 608
ventaquil Avatar asked Apr 27 '15 13:04

ventaquil


1 Answers

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 
like image 160
Margus Pala Avatar answered Sep 21 '22 19:09

Margus Pala