Support the ongoing development of Laravel.io →
Requests Eloquent
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Laravel: Nested resources

But if you just want the answer:

Route::resource('client.dealer.branch', 'YourController');
Last updated 2 years ago.
0

@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.

Last updated 2 years ago.
0

@barryvdh

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?

0

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.

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

iamgerwin iamgerwin Joined 9 Dec 2013

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.