Home / Snippets / Laravel Blade Check if View Exists
Laravel Blade Check if View Exists cover

Laravel Blade Check if View Exists

2.6K

3 years ago

0 comments

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.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this