How To Create Naming Resource Route Parameters In Laravel

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Laravel resource controllers provide the CRUD routes to the controller in a single line of code. A resource controller is used to create a controller that handles all the http requests stored by your application. The resource() is a static function like get() method that gives access to multiple routes that we can use in a controller.

How To Create Naming Resource Route Parameters In Laravel

Naming Resource Route Parameters


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}
 

Example(1)

Route::resource() method generates the route parameters for all the resource routes automatically, but we can override the route parameters by using the parameters array. The parameters array is an associative array of the resource name and route parameters.
 

  • We can override the route parameters by adding the following code in web.php file:

Route::resource('demo', 'demoController', ['parameters' => ['demo' => 'user_demo']]); 

Related Post