Laravel Blade If Condition Example

Laravel Blade If Condition Example

In this example we will show laravel blade if condition example. We will explain you how to use if condition in laravel blade file. We will explain step by step example of laravel blade if multiple conditions.

We will show if condition in laravel blade. Let’s discuss about laravel if condition in view. if statements using the @if, @elseif, @else, and @endif directives.

Read Also : Laravel Blade If Multiple Conditions Example

I will give you simple example of how to write if else statement in laravel 6, laravel 7 and laravel 8 example.

Laravel Blade If Condition :

Syntax:

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

Create users.blade.php view file

@if($isAdmin == 1)
    Admin User
@endif

Create UserController.php controller file

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
  public function index()
  {
    $isAdmin = 1;

    return view('users', compact('isAdmin'));
  }

}

Laravel Blade If Else Condition :

Syntax:

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

Create users.blade.php view file

@if($status == 1)
    Active
@else
    InActive
@endif

Create UserController.php controller file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
  public function index()
  {
    $isAdmin = 1;

    return view('users', compact('isAdmin'));
  }

}

Laravel Blade If Else If Condition :

Syntax:

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

Create users.blade.php view file

@if($status == 1)
    Active
@elseif($status == 2)
    Waiting
@else
    InActive
@endif

Create UserController.php controller file

Read Also :  How to Get App Directory Path in Laravel
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
  public function index()
  {
    $status = 3;

    return view('users', compact('status'));
  }

}

I hope you understand of laravel blade if condition and it can help you…