Posts Learn Components Snippets Categories Tags About

Laravel Blade Check if View Exists

Get to know how to check if laravel blade view exists before rendering

Created on Aug 01, 2021


To check if the view exists before actually rendering it to the page, you can make use of the "exists()" method available from the "view()" helper.

if (view()->exists('partials.heading')) {
    return view('partials.heading', ['data' => $yourHeadingData]);

If you have multiple blade views you can pass it to the "first()" method as an array and whichever view file exists will be loaded by Laravel.
return view()->first(['partialas.heading', 'partials.heading-alt'], ['data' => $yourHeadingData]);

By having those options now you can conditionally load the views whenever necessary.

Snippets by

PostSrc poster


Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

Load comments for Laravel Blade Check if View Exists


PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets