How to Use If, Else & Elseif Condition In Laravel Blade?

January 24, 2023
Laravel
How to Use If, Else & Elseif Condition In Laravel Blade?

In this tutorial, you will learn how to use if and else condition the laravel blade. After completing this small tutorial, you will be able to write if else conditions in laravel blade.

So lets get started!

How to write If Condition in laravel blade file?

To write if condition in laravel blade file, you just have to type @if and @endif , here is the syntax on how to do it:

Syntax:

@if (condition)
     // only code you want to run if the condition is true
@endif

Example:

@if($isGuest==true)
    echo "this is guest user"
@endif

How to write If & else Condition in laravel blade?

To write if and else condition in laravel blade file, you just need to type @if ,@else and @endif. Here is the syntax on how to do that:

Syntax:

@if (condition)
     // only code you want to run if the condition is true
@else
    // only code which you want to run if condition is not true
@endif

Example:

@if($isGuest==true)
    echo "this is guest user"
@else
    echo "this is not guest user"
@endif

How to write If & elseif Condition in laravel blade?

In this part, you will see how we can write if and elseif condition in laravel blade file. Here is syntax of it.

Syntax:

@if (condition)
     // only code you want to run if the condition is true
@elseif(condition2)
    // only code which you want to run if condition 2 is true
@endif

Example:

@if($isGuest==true)
    echo "this is guest user"
@elseif($userName=="Adam")
    echo "this is not guest user but its Adam"
@endif

Conclusion

Hope in the above article, you have learned how to use if , else and elseif in the laravel blade file. See you in next one. Cheers!

Tags

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon