Laravel Routing: Route Model Binding

Route Model Binding

Provides a convenient way to automatically inject the model instances directly into your routes.

Implicit Binding

Route::get('api/users/{user}', function (App\User $user) {
    return $user->email;
});

Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

Laravel automatically inject the model instance that has an ID matching the corresponding value from the request URI.

Explicit Binding

Public function boot()
{
    parent::boot();

    Route::model('user', App\User::class);
}
Route::get('profile/{user}', function (App\User $user) {
    //
});

To register an explicit binding, use the router's model method to specify the class for a given parameter.

Laravel Routing: Route Model Binding — Structure map

Clickable & Draggable!

Laravel Routing: Route Model Binding — Related pages: