Dynamic View In Laravel Using view()->exists() And view()->first()

Dynamic view in laravel using view()->first() is a new frature in laravel 5.5.In the existing verions we can not ckeck if the multiple view blade template is exist or not and set a priority for view pages in laravel with .But now in laravel 5.5 we can ckeck if the view blade template is exist or not and also if the first option view is not exist then load second option and is second oprion veiw is not exist then load third option and so on.we can use view()->exists() And view()->first() for check view file is exist or not in folder.


1.view()->exists() method in laravel

In view()->exists() method we can check if the view page is exist or not in the view folder.in some cases we want to check view page is exist or not.For example,if i want to load product detail page if exist, Othorwise want to load error page in this case we can use this method.

if (view()->exists('custom-template')) {
    return view('product-detail-page', $data);
}

return view('eroor-page', $data);

2.view()->first() method in laravel

Laravel 5.5 introduces the view()->first() method for check if view file is exist or not in laravel in a simple way.if we want to checking multiple view page is exist or not and if exist execute that view page,in that situations we should use multiple view->first() methods.With the use of view->first() method we can done this in simple way.

return view()->first(
    ['product-detail-page', 'eroor-page'], $data
);

Similarly we can use multiple view page checking like below

return view()->first([
    "product/product-{$product->id}",
    "category/category-{$category->id}",
    "pages/eroor-page"
], $data);

In the abow example if doruct detail page with given id is exist then execute the product page.If not exist try to load category page othorwise loads error view template.This can be also use with the facade.

\View::first($viewpages, $data)