How to change resource route name in laravel

Resource controller is amazing for CRUD operation in laravel. But sometimes need to overwrite the default functionality let’s see

How to overriding resource route names

Default resource route is

Route::resource('vendor', 'VendorController');

in case of the route, names are

POST         | vendor                 | vendor.store 
GET|HEAD     | vendor                 | vendor.index 
GET|HEAD     | vendor/create          | vendor.create 
DELETE       | vendor/{vendor}        | vendor.destroy 
PUT|PATCH    | vendor/{vendor}        | vendor.update 
GET|HEAD     | vendor/{vendor}        | vendor.show 
GET|HEAD     | vendor/{vendor}/edit   | vendor.edit

Resource controller use these methods

  • index()
  • create()
  • store()
  • edit()
  • update()
  • show()
  • destroy()

Adding a route name option to the route like this:

Route::resource('vendor', 'vendorController' , ['as' => 'user']);
POST         | vendor                 | user.vendor.store 
GET|HEAD     | vendor                 | user.vendor.index 
GET|HEAD     | vendor/create          | user.vendor.create 
DELETE       | vendor/{vendor}        | user.vendor.destroy 
PUT|PATCH    | vendor/{vendor}        | user.vendor.update 
GET|HEAD     | vendor/{vendor}        | user.vendor.show 
GET|HEAD     | vendor/{vendor}/edit   | user.vendor.edit

Similarly, If you want to change the full name of the route so use this method

Route::resource('vendor', 'VendorController', ['names' => [
'create' => 'my.name',
]]);

Leave a Comment