Linking multiple URL parameters to models is certainly possible in Laravel, just like linking a nested route model.
One caveat is that you will need to specify routes individually for these specific routes, rather than using the Laravel resource controllers, as you used in your example.
To register a route with more than one model binding, you need to do something like this:
Route Definition:
Route::get('/api/client/{user}/journey/{journey}', [ 'as' => 'client.journey', 'uses' => 'JourneyController@getJourney' ]);
Binding Definitions:
$router->bind('user', function($value, $route) { return \App\Client::where('username', '=', $value)->firstOrFail(); }); $router->bind('journey', function($value, $route) { return \App\Journey::where('user_journey_id', '=', $value)->firstOrFail(); });
Then you will find that both models solve and introduce the action method in the controller class:
public function show(User $user, Journey $journey) {
The only real drawback to this approach is the fact that you need to define routes manually instead of using the convenient Laravel resource controllers. Of course, you could implement some of your own logical solutions to simplify these types of routes.
David smith
source share