Laravel Blade @includeWhen and @includeUnless Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Include Blade File with Data Example
- Laravel Blade Include File Example
- Laravel Blade @unless Directive Example
- Laravel Blade Empty Directive Example
- Laravel Blade Isset Else Example
- Laravel Blade Foreach Loop Example
- Laravel Blade Switch Case Statement Example
- Laravel Blade If Multiple Conditions Example