search

Home  >  Q&A  >  body text

How to call Controller using Route::inertia() facade?

I can serve the page using inertia like this:

Route::inertia('/home', 'home');

To load a page containing database data I have to do this:

Route::get('/terms', [LegalPageController::class, 'index']);

In controller:

class LegalPageController extends Controller
{
    public function index()
    {
        $record = Terms::first();

        return inertia('terms', compact('record'));
    }
}

Is there any way to shorten it to:

Route::inertia('/home', 'home', [Controller::class, 'index']);

P粉344355715P粉344355715273 days ago480

reply all(1)I'll reply

  • P粉293341969

    P粉2933419692024-03-29 09:23:19

    You can’t do this

    Route::inertia('/home', 'home', [Controller::class, 'index']);
    

    Use this method instead

    #routes/web.php
    Route::get('home', [Controller::class, 'index']);
    
    
    #App\Http\COntrollers\Controller.php
    namespace App\Http\Controllers
    
    class Controller extends Controller {
        # ...
       Inertia::render('home');
    }
    

    reply
    0
  • Cancelreply