1. Resource Controller And Normal Controller
php artisan make:controller ProductController
After running the above command a new file will be created in the Controllers directory and it will look like the below.
You can notice there are no pre-defined methods generated by the Laravel.
Normal Laravel Controller
app/Http/Controllers/ProductController.php
Let’s now create a ProductController with resource option. The resource option will add all the default methods to the controller.
We will also pass the model option so that it will attach the type hinting along with the resource method.
To create a resource controller with a model run the below command:
php artisan make:controller ProductController --resource --model=Product
After running the above command a new file will be created in the Controllers directory and it will look like the below:
php artisan make:controller StudentsController
Resource Laravel Controller
app/Http/Controllers/ProductController.php
2. Create Resource Routes And Normal Routes
To build the CRUD web app – we need to add all routes manually into the web.php file like below:
Normal Laravel CRUD Routes
routes/web.php
group(function(){
Route::get('products', 'index')->name('products.index');
Route::post('products', 'store')->name('products.store');
Route::get('products/create', 'create')->name('products.create');
Route::get('products/{product}', 'show')->name('products.show');
Route::put('products/{product}', 'update')->name('products.update');
Route::delete('products/{product}', 'destroy')->name('products.destroy');
Route::get('products/{product}/edit', 'edit')->name('products.edit');
});
Now, let’s add the Laravel resource routes in the routes/web.php file to perform the Laravel CRUD operation.
Resource Laravel Routes
routes/web.php
This single route declaration creates multiple routes to handle a variety of actions as defined in below 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 |
3. Laravel Route Resource Except & Only
Sometimes, you don’t want to use all the Laravel resource methods & routes. So if you want to exclude any laravel method or route then you can use the only() and the except() method.
Exclude Index & Show route
while declaring the resource routes you can use the except() method to exclude the index & show the route as below:
use App\Http\Controllers\PhotoController;
Route::resource('photos', PhotoController::class)->except([
'index', 'show'
]);
Use Only Create, Update & Show route
The laravel only() method helps you to show only the required methods.
You can use it the same way as except() method:
use App\Http\Controllers\PhotoController;
Route::resource('photos', PhotoController::class)->only([
'create', 'update', 'show'
]);