ItSolutionStuff.com

Laravel Blade @includeWhen and @includeUnless Example

By Hardik Savani • April 16, 2024
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Include Blade File with Data Example

Read Now →

Laravel Blade Include File Example

Read Now →

Laravel Blade @unless Directive Example

Read Now →

Laravel Blade Empty Directive Example

Read Now →

Laravel Blade Isset Else Example

Read Now →

Laravel Blade Foreach Loop Example

Read Now →

Laravel Blade Switch Case Statement Example

Read Now →

Laravel Blade If Multiple Conditions Example

Read Now →