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


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