How to Create a Resource Route in Laravel 10?

Mar 13, 2023 . Admin


Hi dev,

In this example, you will learn how to create a resource route laravel 10. let’s discuss about laravel 10 resource routing example. you will learn controller. if you have question about laravel 10 routing – laravel 10 routing example tutorial then i will give simple example with solution.

We will step by step process of how to create first route in laravel and understanding of laravel routing with brief.

Let's start following example:

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create CRUD Route:
<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ProductController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

As you can see above route declare, we have to create six routes for our crud application module. But we can simply create those six routes by using bellow resource route:

Step 3: Create Resource Route: Resource Route:
<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ProductsController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::resource('products',ProductsController::class);

Now, you can run bellow command and check create route lists:

php artisan route:list

Now we have output as like bellow created route:

Ok, now we have to create resource controller by using bellow command, so let's run bellow command and check ProductController in your app directory:

Step 4: Create Controller:
php artisan make:controller ProductController --resource --model=Product

After successfully run above command, you can see your ProductController with following resource method, So let's open and see.

app/Http/Controllers/ProductController.php
<php
  
namespace App\Http\Controllers;
  
use App\Models\Product;
use Illuminate\Http\Request;
  
class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
          
    }
  
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
          
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
          
    }
  
    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Product  $Product
     * @return \Illuminate\Http\Response
     */
    public function show(Product $Product)
    {
          
    }
  
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Product  $Product
     * @return \Illuminate\Http\Response
     */
    public function edit(Product $Product)
    {
          
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Product  $Product
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Product $Product)
    {
          
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Product  $Product
     * @return \Illuminate\Http\Response
     */
    public function destroy(Product $Product)
    {
          
    }
}

Ok, this way you can simply use resource route and controller, make quick crud module for your project.

I hope It can help you...

#Laravel 10