This post is from the professional Laravel developer community, original link: learnku.com/laravel/t/3…
In the next major release of Laravel coming in February 2020, you can customize implicit routing model bindings directly in route definitions:
Route::get('/posts/{post:slug}'.function (Post $post{/ /... });Copy the code
Currently, with Laravel 6, the following requirements require you to define a getRouteKeyName() method on the model like this:
<? php class Post extends Model { /** * Get the route keyfor the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug'; }}Copy the code
You can still use the getRouteKeyName() method; However, I think it would be smoother to customize it directly in the route.
You may have multiple routes that you want to bind in different ways. For example, a foreground route uses slugs to display posts, and a background route wants to manage posts by ID
Route::get('/posts/{post:slug}'.function (Post $post{/ /... }); // Alternatively you can use the default '{post}' Route::get('/admin/posts/{post:id}/edit'.function (Post $post{/ /... });Copy the code
If you start experimenting with custom implicit routing model bindings, you can install the development version of Laravel
laravel new example --dev
Copy the code
Filed in: News