Laravel defines put / patch route as the same route name

In Laravel, itโ€™s quite convenient to quickly generate a load of routes using a route resource:

Route::resource('things'ThingsController'); 

This will create all the necessary RESTful routes for CRUD operations. One of them is the PUT / PATCH route, which can be defined as follows:

 PUT/PATCH things/{id} ThingsController@update things.update 

I read that itโ€™s better to explicitly define each of your routes, rather than using a route resource, but how would I define the PUT / PATCH route above. I understand what I can do

 Route::put('thing/{id}', ['as' => 'things.update']); 

or

 Route::patch('thing/{id}', ['as' => 'things.update']); 

But the second overwrites or conflicts with the first, allowing the things.update route things.update to refer only to a PUT or PATCH request. How can I explicitly create a combined PUT / PATCH route created by a resource route?

+5
source share
1 answer

After a tedious search, try the following:

 Route::match(array('PUT', 'PATCH'), "/things/{id}", array( 'uses' => ' ThingsController@update ', 'as' => 'things.update' )); 

This allows you to limit the query through an array of verbs.

Or you can limit the resource this way;

 Route::resource('things', 'ThingsController', array( 'only' => array('update'), 'names' => array('update' => 'things.update') )); 

Both should provide the same result, but please note that they are not tested.

+4
source

Source: https://habr.com/ru/post/1208430/


All Articles