Table of contents
In this post, I will show you an example of Laravel 8 If Else Condition Blade. This is most important to learn when studying Laravel because we will use it to put logic in our Laravel application.
Â
Example #1: if..endif Condition
Below is our sample of if..endif condition for our blade.
Â
Syntax:
@if (condition)
// Statements inside body of if
@endif
Â
Example:
@if (count($posts))
You have a post!
@endif
Â
Example #2: if..else..endif Condition
Below is our example of if..else..endif condition this is helpful if the condition is false then show else result.
Â
Syntax:
@if (condition)
// Statements inside body of if
@else
//Statements inside body of else
@endif
Â
Example:
@if (count($posts))
You have a post!
@else
You don't have a post!
@endif
Â
Example #3: if..elseif..else..endif Condition
Now let's do the multiple condition. See below example:
Â
Syntax:
@if (condition)
// Statements inside body of if
@elseif (condition)
// Statements inside body of else if
@else
//Statements inside body of else
@endif
Â
Example:
@if (count($posts) == 1)
You have a post!
@elseif(count($posts)>1)
You have more than one posts!
@else
You don't have a post!
@endif
Â
That's it you have now the basic knowledge of Laravel if else condition. I hope it helps :)
Read next