Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Matt Stauffer:
Implicit route model binding in Laravel 5.2
Dec 18, 2015 @ 15:34:53

Matt Stauffer has continued his series looking at the new features of Laravel v5.2. In this new article he talks about the addition of implicit route model binding. This is a bit more fancy term for the ability to link models to routes and tell Laravel they're related.

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. [...] In your route service provider, just teach the router: $router->model('shoe', 'AppShoe'); That means, "any time I have a route parameter named shoe, it's an ID representing an instance of AppShoe". [...] 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.

He includes code examples of the "old way" to do it with model functionality inside the route and the "new way" of linking the model to the route. This allows you to auto-magically have a valid model instance once the route is called. He also shares some of the little known features of this model binding including using closures in bind() calls and changing the "route key" away from the "id" column.

tagged: model route binding laravel feature implicit feature v52

Link: https://mattstauffer.co/blog/implicit-model-binding-in-laravel-5-2


Trending Topics: