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

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.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 80 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials