Laravel Resource Controller

How to Creare Resource Controller

You can create the resource controller using following artisan command

This resource controller will have the CRUD methods as shown in the following table

Verb URI Action Route Name
GET /products index products.index
GET /products/create create products.create
POST /products store
GET /products/{product} show
GET /products/{product}/edit edit products.edit
PUT/PATCH /products/{product} update products.update
DELETE /products/{product} destroy products.destroy

Have a look at the generated app/Http/Controllers/ProductController.php file

Now you can register a resourceful route to the controller which you have created above. You can simply run the following code

Although this single route has single route description it has the seven routes as shown in above table

Attach Resource Model

You can create the resource Model as you create the resource router

Remove Action From Resource Route

When you create Resource Route it will have several actions but in some cases you will not need all the actions. So you can exclude some of the actions

Resource Routes for API

Laravel has provides some features when you create Resource Routes for API

When you create a route which is consumed by the API you can omit the create and edit actions.

This Route definition will remove create and edit actions.

You can use --api switch to omit create and edit actions when you create the controller with make:controller artisan command