Skip to main content
Tutorial Free

Resource Controllers for API: How to Remove create/edit methods?

July 24, 2017
1 min read
Resource controllers are great for CRUDs, but if we use them for APIs, there are two unnecessary methods - create() and edit(), cause there are no visual forms for it. So how to remove them from routes? There are two ways. First: did you know that you can add another parameter to Route::resource()? Like this:
Route::resource('roles', 'RolesController', ['except' => ['edit', 'create']]);
So, just list the methods you won't use. Or, alternatively, list methods you only want to use:
Route::resource('roles', 'RolesController', [
  'only' => ['index', 'show', 'store', 'update', 'destroy']
]);

Second: From Laravel 5.4.24 we have a new route function apiResource()
Route::apiResource('roles', 'RolesController');
It will form and pass the same only parameter as in example above. Thanks Lasse Rafn for pointing it out on Twitter.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.