DEV Community

Cover image for Laravel 8 If Else Condition Blade Example
Code And Deploy
Code And Deploy

Posted on

Laravel 8 If Else Condition Blade Example

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/laravel-8-if-else-condition-blade-example

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


Enter fullscreen mode Exit fullscreen mode

Example:

@if (count($posts))
    You have a post!
@endif
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example:


@if (count($posts))
    You have a post!
@else
    You don't have a post!
@endif
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

That's it you have now the basic knowledge of Laravel if else condition.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-if-else-condition-blade-example if you want to download this code.

Happy coding :)

Top comments (0)