Switch in Laravel 5 - Blade
In Laravel 5, the Blade templating engine provides a "switch" statement that allows you to compare a variable to multiple values and execute different code for each match. The syntax for a switch statement in Blade is as follows:
@switch($variable)
@case(value1)
// code to execute if $variable == value1
@break
@case(value2)
// code to execute if $variable == value2
@break
@default
// code to execute if $variable does not match any of the cases
@endswitch
Watch a video course
Learn object oriented PHP
It's important to note that each case statement should end with a @break
statement, which tells Blade to exit the switch statement after executing the code for that case. If you forget to include the @break
statement, Blade will continue to execute the code for all remaining cases, even if a match is found.