Laravel 7 will support direct customization of implicit routing model binding in routing definitions


In the next major version of Laravel to be released, it will support direct customization in the route definition Implicit routing model binding

 Route::get('/posts/{post:slug}', function (Post $post) { // ... });

In the current version, to implement similar functions, you need to define in the corresponding model class getRouteKeyName() Method to achieve:

 <? php class Post extends Model { /** * Get the route key for the model. * * @return string */ public function getRouteKeyName() { return 'slug'; } }

Of course, you can still define this method, but it is more flexible and simple to customize directly in the route definition. For example, if you set implicit model binding in multiple routes, it is obviously more convenient to set it directly in the route definition:

 Route::get('/posts/{post:slug}', function (Post $post) { // ... }); // Or you could use the default `{post}` here... Route::get('/admin/posts/{post:id}/edit', function (Post $post) { // ... });

If you want to experience this function, you can install the development version of Laravel locally through the Laravel installer:

 laravel new example --dev

Note: This article is compiled from Larravel News. The original link is: https://laravel-news.com/implicit-route-model-binding


give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Lightweight user authentication solution for single page applications

>>Next: Introduction to the release cycle of new versions after Larravel 6