Using the command line, you can write the normal command to create the controller but make sure to add the "--invokable" flag.
php artisan make:controller ShowPost --invokable
The generated controller will be as follows.
<?php namespace App\Http\Controllers; use App\Models\Post; use Illuminate\Http\Request; class ShowPost extends Controller { /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function __invoke(Request $request) { return view('posts.show', ['post' => Post::first()]); } }
Leave a reply