Laravel Blade Include File If Exists Example

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