Laravel Route Model Binding Explicit Binding


Example

To register an explicit binding, use the router's model method to specify the class for a given parameter. You should define your explicit model bindings in the boot method of the RouteServiceProvider class

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

   Route::model('user', App\User::class);
}

Next, we can define a route that contains {user} parameter.

$router->get('profile/{user}', function(App\User $user) {
  
});

Since we have bound all {user} parameters to the App\User model, a User instance will be injected into the route. So, for example, a request to profile/1 will inject the User instance from the database which has an ID of 1.

If a matching model instance is not found in the database, a 404 HTTP response will be automatically generated.