Jens Segers on Jun 01 2014

Laravel route model binding is awesome

If you haven't looked at Laravel's route model binding, you should definitely do it. Model binding allows you to inject model instances into your routes. This way, route parameters will automatically be converted to the model that matches the given id. Take the following route for example:

Route::get('profile/{user}', '[email protected]');

With model binding, Laravel will inject the user model, instead of the id from the URL. These bindings are defined like this:

Route::model('user', 'User');

You controller method will then look like this:

public function showProfile(User $user)
{
    return View::make('profile', ['user' => $user]);
}

Instead of:

public function showProfile($id)
{
    $user = User::find($id);

    return View::make('profile', ['user' => $user]);
}

Pretty cool right? It makes your controller code cleaner, and it will also automatically throw a 404 error if the matching model instance is not found in the database.

If you wish to use your own resolver, and/or do some additional security checks, you can define a custom binding:

Route::bind('user', function($value, $route)
{
    $user = User::where('name', $value)->firstOrFail();

    if ($user-> isSuperUser()) App::abort(403, 'Unauthorized action.');

    return $user;
});

I usually group all my bindings in app/bindings.php. To make sure this file gets loaded, add this to the bottom of app/start/global.php:

/*
|-------------------------------------
| Require Route Bindings File
|-------------------------------------
*/

require app_path().'/bindings.php';

You can read more about route model binding in the Laravel documentation: http://laravel.com/docs/routing#route-model-binding


Comments

desjob 2 years ago

Route model binding is awesome indeed! I was looking for a similar way to bind models based on request/POST data. For instance, if youre posting a Book form to /book/ that includes an author_id, its possible to bind the author_id to the Author model in a similar way. I created a RequestServiceProvider to do this: https://gist.github.com/desjob/485f03b22341748926ac


Paul 3 years ago

Very nicely explained. Thanks.


Cas 3 years ago

You can also place them in your filters.php


qwertynesia 3 years ago

nice post!


Yazeed 4 years ago

Thanks for responding, I understand how it works now. Can you answer my other question? :)


Jens Segers 4 years ago

@Yazeed, bindings are logic between the routing system and your controller methods. These model bindings receive the url parameter and convert it to a model object that is then passed to your controller method.


Yazeed 4 years ago

Is this blog running on medium platform?


Yazeed 4 years ago

Ok, I figured this out and it's working great, but for some reason adding the require bindings file in global.php did not work, I had to add the require at the end of my routes.php.


Yazeed 4 years ago

Let me get this straight, binding is so you can look up the user based on name instead of ID?