Laravel — P51: Route Model Implicit Binding

Let Laravel fetch your models automatically

Now that we’re familiar with dependency injection, what else can we do with the concept? Can we “inject” our models, like the User model, into our route and then utilize model properties without instantiating the User model? How would we even do that? Don’t we need to first “find” the specific user before we can use the properties associated with that user?

Believe it or not, Laravel has a way to get a user’s details without explicitly doing something like: User::find(24);.

Implicit Binding

We should understand that we do need to provide our model with an id at the very least, otherwise, how will it pull the information for our specific resource out of the database table? In the User example, we don’t want Laravel to return a random user, we want a specific user.

use App\Models\User;

Route::get('/implicit-binding/{user}', function (User $user) {
    return $user;
});

If we visit the route, you should get something like this.

{
  "id": 2,
  "name": "Maria Nitzsche",
  "email": "colt21@example.org",
  "email_verified_at": "2023-02-07T18:56:39.000000Z",
  "created_at": "2023-02-07T18:56:39.000000Z",
  "updated_at": "2023-02-07T18:56:39.000000Z"
}

What happened? Laravel implicitly bound {user} to $user. Since we type-hinted User $user, Laravel knew that we’re injecting a User model, so it automatically found our User with the specific id.

It doesn’t matter what your name your arguments, as long as they match. With implicit binding, the id will be the field that Laravel searches through, when the parameter is type-hinted.

So, in case you wanted to do something like this (name your URL parameter id), it won’t work:

use App\Models\User;

Route::get('/implicit-binding/{id}', function (User $user) {
    return $user;
});

You’ll just get [] as the response.

But what if we wanted to use another field, like name? We can do that, but it requires that the key is stated.

Changing the Key

If we wanted to instead look for a user based on their name or email, we have to state that that’s the column we want to look into. How do we do that? By stating the key in our route.

use App\Models\User;

Route::get('/implicit-binding/{user:name}', function (User $user) {
    return $user;
});

We can now visit a route like the following:

http://0.0.0.0/implicit-binding/Maria%20Nitzsche

Laravel will search the users table and will return that resource.

{
  "id": 2,
  "name": "Maria Nitzsche",
  "email": "colt21@example.org",
  "email_verified_at": "2023-02-07T18:56:39.000000Z",
  "created_at": "2023-02-07T18:56:39.000000Z",
  "updated_at": "2023-02-07T18:56:39.000000Z"
}

What if you wanted to show only a user if the email has been verified? This is the concept of explicit binding and will be covered next time.

Laravel Series

Continue your Laravel Learning.

Laravel — P50: Routing and Dependency Injection

Combine smart routing with seamless DI

Laravel – P50: Routing and Dependency Injection

In part 50 of our Laravel series, see how routing and the framework’s dependency injection container work together. Learn to inject services directly into route closures and controllers to keep code clean, testable, and flexible.

Laravel — P51: Route Model Implicit Binding

Let Laravel fetch your models automatically

Laravel – P51: Route Model Implicit Binding

In part 51 of our Laravel series, master route‑model implicit binding. Discover how Laravel automatically resolves models from route parameters, simplifies controller logic, and adds type‑safe, readable code to your applications.

Laravel — P52: Explicit Route Model Binding

Take full control of model resolution in your routes

Laravel – P52: Route Model Explicit Binding

In part 52 of our Laravel series, explore explicit route‑model binding to gain precise control over how route parameters resolve to Eloquent models. Learn custom key selection, fallback handling, and advanced security for clean, predictable routing.

 

Leave a Reply