Resource Controller with Resource Route in Laravel

In this short post, we will go over on How to create a resourceful controller in Laravel and also how to define a single line resource route in the web.php file.

Resource Controller

With Laravel, we get the option to create a CRUD controller, which has blank methods for all the default CRUD operations (Create, Read, Update, Delete) by just executing a single artisan command.

php artisan make:controller productController --resource

This artisan command will create a new controller with the name productController which has all the default CRUD methods.

If you are looking to create a resource controller when generating the Model file. Use this command.

Livewire Component Library
php artisan make:model product -r

This artisan command will create a model Product.php, and a resource controller productController.php.

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.

Resource Route

Here is how you can define the resource route in the web.php file

Route::resource('products', 'ProductController');

This code line works like magic, it will automatically assign many action verbs to the resource controller. Since we generated a resource controller, this line will internally map route names to a different controller actions.

Verb URI action (Controller method) 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 tasks.edit
PUT/PATCH /products/{product} update products.update
DELETE /products/{product} destroy products.destroy

Site Footer