Is it possible to have route model binding using multiple parameters? For example
Web Routes:
Route::get('{color}/{slug}','Products@page');
So url
www.mysite.com/blue/shoe
will be binded to shoe
Model, which has color
blue.
Is it possible to have route model binding using multiple parameters? For example
Web Routes:
Route::get('{color}/{slug}','Products@page');
So url
www.mysite.com/blue/shoe
will be binded to shoe
Model, which has color
blue.
First of all, it would feel more natural to have a route like the following:
and to resolve
product
by route binding, and just use thecolor
parameter in the controller method directly, to fetch a list of blue shoes for example.But let's assume that for some reason it's a requirement. I'd make your route a bit more explicit, to start with:
Then, in the
boot
method ofRouteServiceProvider.php
, I would add something like this:first
here is important, because when resolving route models like that you effectively want to return a single model.That's why I think it doesn't make much sense, since what you want is probably a list of products of a specific color, not just a single one.
Anyways, I ended up on this question while looking for a way to achieve what I demonstrated above, so hopefully it will help someone else.
Try changing your controller to this:
You must add the Page Model.