Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel add custom method to resource controller

I'm using laravel 5.2 and I wanted to know if there's an option to include into the resource more methods.

for example, Id'e like to create a POST method called getUsersList which I can limit the results. I know I can just add in the routes separately from the resource a new route, but I would need to do this for every route I do.

What's the best way to do this?

like image 280
Eliya Cohen Avatar asked Sep 02 '25 04:09

Eliya Cohen


1 Answers

Of course you can add new actions (methods) to RESTful controllers.

Just add method and create the route for this action:

Route::post('foo/bar', 'FooController@bar');

And don't forget to put this route before RESTful route:

Route::post('foo/bar', 'FooController@bar');
Route::resource('foo', 'FooController');
like image 188
Alexey Mezenin Avatar answered Sep 04 '25 18:09

Alexey Mezenin