Easy Implicit Route Model Binding Coming to Laravel 7

Easy Implicit Route Model Binding Coming to Laravel 7

In the next major release of Laravel coming in February 2020, you can customize implicit route model bindings directly in the route definition:

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

Currently, Laravel 6 and below requires you to define a getRouteKeyName() method on the model like so:


class Post extends Model
     * Get the route key for the model.
     * @return string
    public function getRouteKeyName()
        return 'slug';

You will still be able to use the getRouteKeyName() method; however, I feel like customizing it directly in the route definition makes it more flexible.

Perhaps you have multiple routes that you want to bind differently. For example, the frontend route uses slugs to display posts and backend admin uses ids to manage posts:

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 start experimenting with customizing implicit route model binding, you can install the development version of Laravel:

laravel new example --dev

Filed in: News


Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Platform Engineer
Intermediate Laravel Developer
Austin, TX
Elevation Energy Group
Sr. PHP Developer
Las Vegas
William Hill
Full Stack Laravel Vue Developer
Remote / Bay Area
ZAM Apps, Inc.
Senior Backend PHP Developer (Symfony) (Remote)
Project Admission
Intermediate Laravel Developer (No Remote, Toronto Only)
Toronto, Canada
Inkbox Ink Inc
Looking for Senior Full-stack Laravel/React.js Javascript API Developer - Immediate (Full-time)
ApproveMe // Document Signing