Laravel 8 - If condition in blade Example

147207 views 1 year ago Laravel

In this article, I will share with you how to manage If elseif and else condition in laravel blade file. as you all know laravel is a provide manu easy to use functionality to use more make application code more less. in this example we see how to manage laravel blade file HTML content base on if, elseif or else condition base.

If Condition Example

Syntax

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

Blade File Example

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

Controller File Example

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function index()
    {
        $isAdmin = 1;
        return view('ajaxRequest', compact('isAdmin'));
    }
}

If - else Condition

Syntax

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

Blade File Example

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

Controrller File Example

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function index()
    {
        $status = 1;
        return view('ajaxRequest', compact('status'));
    }
}

If - elseif -else Condition

Syntax

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

Blade File Example

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

Controller File Example

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function index()
    {
        $status = 3;
        return view('ajaxRequest', compact('status'));
    }
}

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]