Home > Article > PHP Framework > About simple implicit routing model binding in Laravel 7
In the next major release of Laravel, you can customize implicit routing model binding directly in the routing definition:
Recommended: laravel tutorial
Route::get('/posts/{post:slug}', function (Post $post) { // ... });
Currently, using Laravel 6, the requirements below require you to define a getRouteKeyName() method on the model like this:
<?php class Post extends Model { /** * Get the route key for the model. * * @return string */ public function getRouteKeyName() { return 'slug'; } }
You can still use the getRouteKeyName() method; however , I think it would be smoother to customize it directly in the route.
Maybe you will have multiple routes that you want to bind in different ways. For example, the frontend router uses slugs to display posts, and the backend hopes to manage posts by ID
Route::get('/posts/{post:slug}', function (Post $post) { // ... }); // 或者你在这儿可以用默认的`{post}` Route::get('/admin/posts/{post:id}/edit', function (Post $post) { // ... });
If you start trying to customize implicit routing model binding, you can install the development version of Laravel
laravel new example --dev
The article is forwarded from the professional Laravel developer community, original link: https://learnku.com/laravel/t/37702
The above is the detailed content of About simple implicit routing model binding in Laravel 7. For more information, please follow other related articles on the PHP Chinese website!