api

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:

So, just list the methods you won’t use. Or, alternatively, list methods you only want to use:


Second: From Laravel 5.4.24 we have a new route function apiResource()

It will form and pass the same only parameter as in example above.

Thanks Lasse Rafn for pointing it out on Twitter.

Want more tips and articles like this?
Subscribe to our weekly newsletter - comes out every Thursday!
Join 1200+ Laravel professionals who read the issue weekly.

Leave a Reply

Your email address will not be published. Required fields are marked *