Laravel Blade Include File If Exists Example
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...
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 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
- Laravel Blade If Condition Example