0

I am working on the Laravel 8 application where I have nested resources URL.

According to my routes definition, A user can have multiple locations and each location can have multiple contents.

Route::resource('locations', LocationController::class);
Route::resource('locations.contents', ContentController::class);

So when I open the URL like

/locations/3/contents/1

it will never check either the content whose id is 1 belongs to their parent(i.e location whose id is 3).

I was expecting that, it should return 404 pages by the Laravel framework as the content model(with id 1) is actually belongs to the location model(with id 2)

So all in all

/locations/1/contents/1 - Should work

/locations/3/contents/1 - Should not work.

Can someone help me with this?

Sachin Kumar
  • 3,001
  • 1
  • 22
  • 47

1 Answers1

0

By specifying the scoped option to the route, it starts working.

I have to set the scoped even it should be automatic by the framework.

mentioned by the Laravel documentation.

The route should be

Route::resource('locations', LocationController::class);
Route::resource('locations.contents', ContentController::class)->scoped([
    'locations' => 'location:id',
    'contents' => 'content:id',
]);
Sachin Kumar
  • 3,001
  • 1
  • 22
  • 47