You can create the resource controller using following artisan command
php artisan make:controller ProductController --resource
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 | products.store |
GET | /products/{product} | show | products.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
Route::resource('products', 'ProductController');
Although this single route has single route description it has the seven routes as shown in above table
You can create the resource Model as you create the resource router
php artisan make:controller ProductController --resource --model=Photo
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
Route::resource('photos', 'ProductController')->only([ 'index', 'show' ]); Route::resource('photos', 'ProductController')->except([ 'create', 'store', 'update', 'destroy' ]);
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.
Route::apiResource('products', 'ProductController');
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
php artisan make:controller API/ProductController --api