Lakmal Premaratne
Lakmal Premaratne

Reputation: 1229

Laravel fallback view

I need to return a default view if a particular view is not available. I expect something like return view(['order.create', 'base.create'], ['params' => $params]); Can I achieve this without having set of if statements in Laravel?

Upvotes: 0

Views: 537

Answers (2)

Tan Nguyen
Tan Nguyen

Reputation: 1844

Simpler Method

  1. Go to app/config/view.php
  2. Under paths, add new view path, for example:
    'paths' => [
        resource_path('base/views'),
        resource_path('views'),
    ],
  1. Use view() function without first() and array.
return view('order.create', compact('params'))

This will load views for base/views directory first, if not found, then find in the views directory. Then you can put blade files and directory with same structure.

Upvotes: 1

IndianCoding
IndianCoding

Reputation: 2683

try this:

return view()->first(['order.create', 'base.create'], ['params' => $params]);

Upvotes: 2

Related Questions