Inside your "App\Models\[your-modal].php" modal define the "getRouteKeyName".
# App\Models\Article.php public function getRouteKeyName() { return 'slug'; }
In your route specify the "article" as the key and by doing so Laravel will use the "slug" key to implicitly retrieve the instance.
# routes/web.php Route::get('/article/{article}', 'ArticleController@show');
In the controller, specify the "article" as the key and type hint in the model instance.
# App\Http\Controllers\ArticleController.php public function show(Article $article) { dd($article); }
Before
# App\Http\Controllers\ArticleController.php public function show($id) { $article = Article::find($id); dd($article); }
After
# App\Http\Controllers\ArticleController.php public function show(Article $article) { dd($article); }
Leave a reply
A
Alaz
8 months ago
Good snippets!
A
Alaz
8 months ago
Loving the snippet!