Laravel 9 Resource Controller And Route With Example

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:

				
					php artisan make:controller StudentsController
				
			

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'
]);

				
			
That’s it from our end. We hope this article helped you with the Laravel 9 resource controller and resource routes.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top