skip to Main Content

I have these view files:

  • about.blade.php
  • home.blade.php
  • contact.blade.php

I want to return all of them using one controller function index():

public function index()
{
    return view(['about', 'contact', 'home']);
}

2

Answers


  1. you can use the view()->first() method to return the first view file that exists in the specified array of views.

    for example:

    public function index()
    {
        return view()->first(['about', 'contact', 'home']);
    }
    
    Login or Signup to reply.
  2. You can use route params for it.

    Define route in web.php:

    Route::get('/{page}', [IndexController::class, 'index'])->name('index');
    

    Then in the controller use this:

    public function index(string $page) 
    {
      
      if (!in_array($page, ['about', 'contact', 'home']) {
        abort(404);
      }
    
      return view($page);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search