Laravel Implicit Binding


Example

Laravel automatically resolves Eloquent models defined in routes or controller actions whose variable names match a route segment name. For example:

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

In this example, since the Eloquent $user variable defined on the route matches the {user} segment in the route's URI, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI. If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated.

If the model's table name is composed from multiple words, to make the implicit model binding working the input variable should be all lowercase;
For example, if the user can do some kind of action, and we want to access this action, the route will be:

Route::get('api/useractions/{useraction}', function (App\UserAction $useraction) {
    return $useraction->description; 
});