Laravel Blade – if else condition in blade view file Example

Laravel if else condition in blade file example; In this tutorial, we will show you how to use if, else if, and if with else if statement condition in laravel blade file.

First time using the Laravel application the beginners don’t know how to add the if elseif else condition in laravel blade file. So here in the below example, we have added the best and easiest example to using @if, @elseif and @else conditions according to the requirements.

Example 1: If Condition Example

This example explains your use of the if condition in the blade file.

Syntax:

@if (condition)
    /* Statements inside body of if */
@endif

Blade File Code:

@if(auth()->user()->hasRole('admin'))         
      <td>
         <a href="#" class="btn btn-primary" >Edit</a>
         <a href="#" class="btn btn-danger">Delete</a>
      </td>         
@else

Example 2: If Else Condition Example

In this example, we use the if and else statements.

Syntax:

@if (condition)
    /* Statements inside body of if */
@else
    /* Else body of if */
@endif

Blade File Code:

@if($user->status == 'active')         
      <td> <a href="#" class="btn btn-danger" >Actived</a></td>         
@else
      <td> <a href="#" class="btn btn-success"> Deactiveted </a></td>  
@endif

Example 3: If Else If Condition Example

You can use the if, elseif, and else condition just like below.

Syntax:

@if (condition)
    /* Statements inside body of if */
@elseif(condition)
    /* if else inside body */
@else
    /* Else body of if */
@endif

Code:

<td>
@if($user->status == 'active')         
       <a href="#" class="btn btn-danger" >Deactive</a>
@elseif($user->status == 'waiting')
        <a href="#" class="btn btn-info" >Waiting</a>
@else
      <a href="#" class="btn btn-success" >Deactive</a>
@endif
</td> 

Hope these examples help you…

Leave a Comment