Sajjad Anghooty
Sajjad Anghooty

Reputation: 13

How to change name of Nested Resource Route in Laravel?

My question is about when I change a resource name like this :

Route::resource('photos', 'Photos\PhotoController')->parameters(['photo' => 'photo_id']);

it works and default "photo" parameter name changes to "photo_id". But when I use nested resource route like this:

Route::resource('photos.captions', 'Photos\PhotoController')->parameters(['photo' => 'photo_id', 'caption' => 'caption_id']);

"caption" parameter name doesn't change to "caption_id".

Is there any way to change both of them? thank you :)

Upvotes: 1

Views: 1399

Answers (1)

Remul
Remul

Reputation: 8252

The following should work:

Route::resource('photos.captions', 'Photos\PhotoController')
    ->parameters(['photos' => 'photo_id', 'captions' => 'caption_id']);

The resource names and the parameters names have to match:

  • resource: photos, parameter: photos
  • resource: captions, parameter: captions

From the docs:

By default, Route::resource will create the route parameters for your resource routes based on the "singularized" version of the resource name. You can easily override this on a per resource basis by using the parameters method. The array passed into the parameters method should be an associative array of resource names and parameter names:

Route::resource('users', 'AdminUserController')->parameters([
    'users' => 'admin_user'
]);

The example above generates the following URIs for the resource's show route:

/users/{admin_user}

Upvotes: 1

Related Questions