Posts Learn Components Snippets Categories Tags Tools 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

2594 views

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.

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for Laravel Blade Check if View Exists

new

PostSrc Code Components

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

View Components

Sponsors 👑

+ Add Yours
)