Table of contents

Laravel If Else

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 :)