Best Way to Exclude Resourceful Functions in Laravel

Hello artisan, in this example i will explain that how to exclude function or method name in a resource controller in laravel. According to source code, method resource is not chainable in laravel, because it returns void

Sometimes we need to chain all resource controller together and need to exclude some function like show, destroy etc. In this case, what do you do to organize your route list? For that i am here to help you.

We can write our own class that extends Illuminate\Routing\Route and implement your resources method like that:

namespace App\Extends;

class Route extends Illuminate\Routing\Route {

   public function resources($routes, array $excepts) {
     foreach ($routes as $key => $value){
         $this->resource($key, $value)->except($excepts);
     }
   }

}

 

After doing that you need to bind class in your service provider like this:

app\Providers\AppServiceProvider.php

public function register()
{
    $this->app->bind('Illuminate\Routing\Route', 'App\\Extends\\Route');
}

 

Now call resources in web.php like that:

routes\web.php

Route::resources([,
    'cateories' => 'CategoryController',
    'posts' => 'PostController',
], ['show']);

 

Now laravel will exclude show method and route from your above resource controller. But from laravel 5.8 we can register many resource controllers at once by passing an array to the resources method:

Route::resources([
    'photos' => 'PhotoController',
    'posts' => 'PostController'
]);

 

And exclude method like that:

Route::resources([])->except(['show'])

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js