Laravel Blade Include File If Exists Example

Laravel Blade Include File If Exists Example; In this tutorial you will learn How to check if a (blade) view file exists in laravel application.

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.

Main Default File

Suppose you have a default file which is showing the index;

resources/views/products/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Blade Include File if exists Example - codingdriver.com</title>
</head>
<body>
    @includeIf('products.details', ['product' => $product ])
</body>
</html>

Include File

Now create a blade file details and inlude it file file exits;

resources/views/products/details.blade.php

<div>
    {{ $product->name }}
</div>

Hope it help you…

Leave a Comment