Conditional (@if, @else, @elseif, @isset, @empty, and @unless) Statements in Laravel.

First of all, we make a condition.blade.php file in which we define our conditions. See the below image.

After that, we make a function named condition in the controller and Defined a variable $name and assigned a string Sushant into it. After that returned it to view(condition.blade.php file is in a test folder). See the below image for help.

After that, we configure our Route. See below how I configured,

@if and @else Condition

Condition wrote under if statement is executed when it’s True otherwise else executed. See the below Example:-

Sample Program

In the above program, if-statement is true so it gives output String is > 0. See the output below:-

Output

@elseif Condition

The else-if statement is written after if and before else statement and it executes when if statement is false. See the below example:-

Sample Program

In the above program, if condition is false so elseif condition checked whether it’s true or false, if it’s true then it executed otherwise else condition executed. But in the above program elseif condition is true, so it’s executed and gives the output:- String is in between 5 and 10. See the output below.

Output

@isset Condition

@isset statement is used to check whether the variable we called is defined or not. If not defined then it’s not executed and prints next statement but doesn’t return an error. See the below example:-

Sample program

In the above program, Variable $string is not defined so @isset statement is not executed and the next statement prints. See the below output

@empty Condition

The @empty statement is used to check whether the called variable has value or not. If no value is assigned to the variable then statement executed otherwise not executed. See the below example:-

Sample Program

In the above program, Variable $name has no value, so when we check $name then it returns statement written under @empty condition.

Output

@unless Condition

It is used to check whether the array or variable we called has any value declared or not. If no value is assigned to the variable or array then statement executed otherwise not executed. See the below example:-

Sample Program

In the above program, there is no value assigned to the variable $name so it returns Value is empty. See the output below:-