Posts Learn Components Snippets Categories Tags Tools About

Adding Other Method to Resource Controller in Laravel

Learn how to add new method to resource controller in Laravel from within the routes/web.php file

Created on Nov 14, 2021


To add another method to the resource you can define a new one below the route resource definition on the "routes/web.php" file. For an example, you can see the command below where it's creating a new controller called "ArticleController" and having the "-r" flag which stands for "resource".
php artisan make:controller ArticleController -r
By default it will have all the "index", "create", "store", "show", "edit", "update" and "destroy". Now if you want to add a new method to the existing resource you can define a new method such as the following.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller
    /* Your other codes here */

    public function trending()
        // your code logic here
Now from your "routes/web.php" file you can define your route resource as follow.

Route::resource('articles', \App\Http\Controllers\ArticleController::class);
Route::get('articles/trending', [\App\Http\Controllers\ArticleController::class, 'trending']);
If you want to view all of your routes available from within the application then you can run the route:list command.
php artisan route:list

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️