Laravel 10 Resource Route and Controller Example

By Hardik Savani November 5, 2023 Category : Laravel

Hi Artisan,

This is a short guide on laravel 10 resource controller. you'll learn laravel 10 resource route. This post will give you a simple example of how to use resource controller in laravel 10. If you have a question about resource route in laravel 10 then I will give a simple example with a solution.

You can simply understand the concept of resource route and controller in laravel 10 application. you need to just follow this tutorial.

Laravel resource controller and resource route are pretty interesting features to create a quick CRUD application in laravel. For resource, you have to do two things on the laravel application. first, you have to create a resource route on laravel that provide insert, update, view, delete routes, and second, you have to create a resource controller that will provide a method for insert, update, view, and delete.

So, in this example, we will see how to create resource routes and how they work.

First, we have to understand why we choose the resource route instead of a different route. we always declare different route for our crud application like as bellow:

CRUD Route:

   

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ItemController;

/*

|--------------------------------------------------------------------------

| 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(ItemController::class)->group(function(){

Route::get('items', 'index')->name('items.index');

Route::post('items', 'store')->name('items.store');

Route::get('items/create', 'create')->name('items.create');

Route::get('items/{item}', 'show')->name('items.show');

Route::put('items/{item}', 'update')->name('items.update');

Route::delete('items/{item}', 'destroy')->name('items.destroy');

Route::get('items/{item}/edit', 'edit')->name('items.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:

Resource Route:

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ItemController;

/*

|--------------------------------------------------------------------------

| 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('items', ItemController::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 ItemController in your app directory:

Resource Controller Command:

php artisan make:controller ItemController --resource --model=Item

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

app/Http/Controllers/ItemController.php

<?php

namespace App\Http\Controllers;

use App\Models\Item;

use Illuminate\Http\RedirectResponse;

use Illuminate\Http\Request;

use Illuminate\Http\View;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*/

public function index(): View

{

}

/**

* Show the form for creating a new resource.

*/

public function create(): View

{

}

/**

* Store a newly created resource in storage.

*/

public function store(Request $request): RedirectResponse

{

}

/**

* Display the specified resource.

*/

public function show(Item $item): View

{

}

/**

* Show the form for editing the specified resource.

*/

public function edit(Item $item): View

{

}

/**

* Update the specified resource in storage.

*/

public function update(Request $request, Item $item): RedirectResponse

{

}

/**

* Remove the specified resource from storage.

*/

public function destroy(Item $item): RedirectResponse

{

}

}

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

I hope It can help you...

Shares