Laravel Blade If Condition Example

In this example we will show laravel blade if condition example. We will explain you how to use laravel blade file in if condition. 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.

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

If Condition Example:

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'));
  }

}

If Else Condition Example:

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'));
  }

}

If Else If Condition Example:

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 :  Laravel 8 CRUD Operation With Ajax Example
<?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 example and it can help you…