ItSolutionStuff.com

Laravel Blade Include File If Exists Example

By Hardik Savani • April 16, 2024
Laravel

Hi,

Today, i would like to show you laravel blade include file if exists. this example will help you include if file exists laravel blade. it's simple example of how to include file if exists in laravel blade. it's simple example of laravel blade include if exists.

if you need to include file if exists in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 then this example will help you.

laravel provide two @include directive. one is @include and another @includeIf. if you use @include then if view not exists then it throw error, but if you use @includeIf then it will not throw error. it will only include when it's exists.

let's see bellow simple example:

resources/views/products/index.blade.php

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

@includeIf('products.alert', ['type' => 'success'])

</body>

</html>

resources/views/products/alert.blade.php

<div class="alert alert-{{$type}}">

This is Alert.

</div>

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()

{

return view('ajaxRequest');

}

}

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 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 →

Laravel Blade If Condition Example

Read Now →