Home » Laravel 9 Resource Controller And Route With Example

Laravel 9 Resource Controller And Route With Example

Last updated on June 11, 2022 by

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:

VerbURIActionRoute Name
GET/productsindexproducts.index
GET/products/createcreateproducts.create
POST/productsstoreproducts.store
GET/products/{product}showproducts.show
GET/products/{product}/editeditproducts.edit
PUT/PATCH/products/{product}updateproducts.update
DELETE/products/{product}destroyproducts.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:

  1. Laravel 9 Image Upload Tutorial With Example
  2. Laravel 9 Multiple Database Connections Example
  3. Laravel 9 Automatically Generates Sitemap With Example
  4. Make Model In Laravel 9 Tutorial With Example
  5. Laravel Firebase Tutorial With Example
  6. Multiple File Upload In Laravel 9 With Example
  7. Laravel 9 Multiple Images Upload Example
  8. Show All Fields Of Model In Django Admin
  9. Laravel 9 Multi Language Routes With Auth Routes
  10. How To Update Pivot Table In Laravel
  11. How To Install Vue In Laravel 8 Step By Step
  12. How To Handle Failed Jobs In Laravel
  13. Best Ways To Define Global Variable In Laravel
  14. How To Get Latest Records In Laravel
  15. Laravel Twilio Send SMS Tutorial With Example
  16. How To Pass Laravel URL Parameter
  17. Set Default Value Of Timestamp In Laravel Migration
  18. Laravel 9 File Upload Tutorial With Example
  19. How To Schedule Tasks In Laravel With Example
  20. 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!

 
 

Leave a Comment