Posts Learn Components Snippets Categories Tags About
/

How to Provide Fallback Views in Laravel

Learn how to return multiple views in Laravel controller to provide a fallback view when one or more views is not available

Created on Aug 17, 2021

7 views

When returning the view file from the controller, you can provide an array of blade views and it will provide a fallback for when one view is not available. To do so you can make use of the "first()" method which accepts an array of blade views as the first parameter and the data to be passed on as the 2nd parameter.
return view()->first(['partials.sidebar-tempate', 'partials.default-template'], $data);
This is very useful when you have a theme system that by default has a default theme and when you provide a new custom view then the system will make use of the custom view instead of the default one.

Snippets by

PostSrc poster

PostSrc

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 How to Provide Fallback Views in Laravel

new

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

More