DEV Community

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

Posted on

4 2

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay