Implicit route model binding in Laravel 5.2
If you've never used it, Laravel's route model binding has been around for a while, but Laravel 5.2 is about to make it even easier.
The basics of route model binding
Let's assume that a common pattern for binding a URL route is something like this:
Route::get('shoes/{id}', function ($id) {
$shoe = Shoe::findOrFail($id);
// Do stuff
});
This is something I do a lot. Wouldn't it be nice if you could drop the findOrFail line and just teach Laravel's router that this route represents a Shoe? You can. In your route service provider, just teach the router: $router->model('shoe', 'App\Shoe'); That means, "any time I have a route parameter named shoe, it's an ID representing an instance of App\Shoe". This allows us to re-write the above code like this:
Route::get('shoes/{shoe}', function ($shoe) {
// Do stuff
});
Implicit route model binding
In Laravel 5.2, it's even easier to use route model binding. Just typehint a parameter in the route Closure (or your controller method) and name the parameter the same thing as the route parameter, and it'll automatically treat it as a route model binding:
Route::get('shoes/{shoe}', function (App\Shoe $shoe) {
// Do stuff
});
That means you can now get the benefits of route model binding without having to define anything in the Route Service Provider. Easy!
That's it for implicit route model binding! Everything past this point is already around in 5.1.
Little known features of route model binding
These features are not new with 5.2, and therefore not specific to implicit route model binding, but they seem to be not commonly known, so I thought I would throw them in here.
Custom binding logic for route model binding
Link:
This post is submitted by our members. Submit a new post.
Tags: Tutorials Laravel 5 Laravel 5.1