I usually do something like this:
// routes.php
Route::model('client', 'Client');
Route::model('dealer', 'Dealer');
Route::model('branch', 'Branch');
Route::resource('client/{client}/dealer/{dealer}/branch', 'BranchController');
//BranchController.php
public function index(Client $client, Dealer $dealer){ .. }
public function show(Client $client, Dealer $dealer, Branch $branch){ .. }
I don't know if that it was you mean? Instead of model binding you can also just use the ID's, but I think this makes it a lot easier.
If I understand you correctly, what you're looking for are nested resources.
Here's a great tutorial by Jeffrey Way that will answer your question.
But if you just want the answer:
Route::resource('client.dealer.branch', 'YourController');
@barryvdh Thanks for the other idea on how to resolve this case. I will also use this solution.
@zanmoskotevc Yup, I'm looking for that tutorial.. I forgot what the name of the video is, thanks for bringing it up.
If I want to use
client/{client.username}/dealer/{dealer.username}/branch
instead of
client/client.id/dealer/dealer.id/branch
how can I achieve this?
This directly matters only if U plan to use Route-Model Binding.
If you would like model binding to use a database column other than id when retrieving a given model class, you may override the getRouteKeyName method on the Eloquent model:
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
In case U aren't using route model binding, feel free to interpret the parameter as any field within the controller method, or the route handler closure.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community