Laravel resource: How define the name of the key parameter?

Viewed 3274

When you define a resource with Route::resource('recipe', 'RecipeController');, among others, the following route is defined: /photo/{photo}/edit, and once you define all your resources you have something like this:

  • /recipes/{recipes}/edit
  • /allergens/{allergens}/edit
  • /ingredients/{ingredients}/edit

Because all my records use id as primary key (MongoDB), I'd like to have {id} instead, like so:

  • /recipes/{id}/edit
  • /allergens/{id}/edit
  • /ingredients/{id}/edit

I dug in the Router class but I don't see how to specify this.

More over when I create a form with Form::model($record) I get actions like /recipes/{recipes} because recipes is a property of $record.

How can I define the name of the key parameter to id instead of recipes, allergens, ingredients?

3 Answers

I know this is 4 year old question but for anyone who is googling; you can pass a third argument to override key naming:

Route::resource('ingredients', 'IngredientController', ['parameters' => ['ingredients' => 'id']]);

Or

Route::resource('ingredients', 'IngredientController')->parameters(['ingredients' => 'id']);

Related