Laravel Blade @includeWhen and @includeUnless Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi,

In this tutorial we will go over the demonstration of laravel blade @includeWhen. i explained simply step by step laravel blade @includeUnless. you will learn laravel blade includeWhen. this example will help you laravel blade include with if condition.

you can easily use @includeWhen and @includeUnless example in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 then this example will help you.

You can write if condition on include file with laravel blade. laravel added two directive where you can write if condition @includeWhen and @includeUnless.

let's see bellow simple example:

Syntax: @includeWhen

@includeWhen(boolean variable, 'view path', array)

Syntax: @includeUnless

@includeWhen(boolean variable, 'view path', array)

resources/views/products/index.blade.php

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

@includeWhen($includeWhenYes, 'products.includeWhen')

@includeWhen($includeUnlessNo, 'products.includeUnless')

</body>

</html>

resources/views/products/includeWhen.blade.php

This is includeWhen Include.

resources/views/products/includeUnless.blade.php

This is includeUnless Include.

Controller File Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AjaxController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function index()

{

$includeWhenYes = true;

$includeUnlessNo = false;

return view('ajaxRequest', compact('includeUnlessNo', 'includeUnlessNo'));

}

}

Output:

This is includeWhen Include.

This is includeUnless Include.

I hope it can help you...

Tags :
Shares