PASSWORD RESET

Your destination for complete Tech news

How to add new methods to a resource controller in Laravel?

798 0
< 1 min read

To add new methods to a resource controller in Laravel, you can simply define the new methods in the controller class.

For example, if you have a resource controller called PostController, you can add a new method called search like this:

class PostController extends Controller
{
    // ... existing methods ...

    public function search(Request $request)
    {
        // search logic goes here
    }
}

Then, to create a route for the search action, you can define a new route in your routes file (e.g., routes/web.php) using the Route::get method:

Route::get('posts/search', 'PostController@search');

This will create a route for the search action that you can access using the /posts/search URL.

Alternatively, you can also use the Route::post method if you want to submit a form to the search action, or the Route::any method if you want the route to be accessible using any HTTP method.

Keep in mind that if you are using resource routes, you will need to either include the new method in the resource routes using the only or except methods, or define a separate route for it as shown above.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.