In this tutorial, we will learn Laravel 9 resource controller and resource route with example. Simply resource will provide you default functionality so that you need to create it manually again and again. Laravel 9 provide a convenient way to create controllers & route with a resource so that we need to develop methods & routes manually for CRUD operations. Let’s dive into it.
1. Resource Controller And Normal Controller
First, we will create a normal controller and then we will create a resource controller in Laravel so that you can easily understand the difference between the two. To create a normal controller run the below command:
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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
}
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:
Resource Laravel Controller
app/Http/Controllers/ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
}
public function create()
{
}
public function store(Request $request)
{
}
public function show(Product $product)
{
}
public function edit(Product $product)
{
}
public function update(Request $request, Product $product)
{
}
public function destroy(Product $product)
{
}
}
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
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::controller(ProductController::class)->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
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
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'
]);
Hurray! we have completed all the steps to learn the Laravel 9 resource controller with resource routes.
Additionally, read our guide:
- Laravel 9 Image Upload Tutorial With Example
- Laravel 9 Multiple Database Connections Example
- Laravel 9 Automatically Generates Sitemap With Example
- Make Model In Laravel 9 Tutorial With Example
- Laravel Firebase Tutorial With Example
- Multiple File Upload In Laravel 9 With Example
- Laravel 9 Multiple Images Upload Example
- Show All Fields Of Model In Django Admin
- Laravel 9 Multi Language Routes With Auth Routes
- How To Update Pivot Table In Laravel
- How To Install Vue In Laravel 8 Step By Step
- How To Handle Failed Jobs In Laravel
- Best Ways To Define Global Variable In Laravel
- How To Get Latest Records In Laravel
- Laravel Twilio Send SMS Tutorial With Example
- How To Pass Laravel URL Parameter
- Set Default Value Of Timestamp In Laravel Migration
- Laravel 9 File Upload Tutorial With Example
- How To Schedule Tasks In Laravel With Example
- Laravel Collection Push() And Put() With Example
That’s it from our end. We hope this article helped you with the Laravel 9 resource controller and resource routes.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!