ItSolutionStuff.com

Laravel 9 Resource Route and Controller Example

By Hardik Savani • November 5, 2023
Laravel

In this article, we will cover how to implement laravel 9 resource controller. I would like to show you laravel 9 resource route. I’m going to show you about how to use resource controllers in laravel 9. I would like to share with you the resource route in laravel 9.

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

Laravel resource controller and resource route is a pretty interesting feature 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 they provide insert, update, view, delete routes and second, you have to create a resource controller that will provide 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\Request;

class ItemController 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\Item $item

* @return \Illuminate\Http\Response

*/

public function show(Item $item)

{

}

/**

* Show the form for editing the specified resource.

*

* @param \App\Models\Item $item

* @return \Illuminate\Http\Response

*/

public function edit(Item $item)

{

}

/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Models\Item $item

* @return \Illuminate\Http\Response

*/

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

{

}

/**

* Remove the specified resource from storage.

*

* @param \App\Models\Item $item

* @return \Illuminate\Http\Response

*/

public function destroy(Item $item)

{

}

}

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

I hope It can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

Laravel 9 Send Email using Queue Example

Read Now →
ā˜…

Laravel 9 Clear Cache of Route, View, Config, Event Commands

Read Now →
ā˜…

Laravel 9 Yajra Datatables Example Tutorial

Read Now →
ā˜…

Laravel 9 REST API Authentication using Sanctum Tutorial

Read Now →
ā˜…

Laravel 9 Send Mail using Gmail SMTP Server

Read Now →
ā˜…

Laravel 9 Markdown | Laravel 9 Send Email using Markdown Mailables

Read Now →
ā˜…

Laravel 9 Import Export Excel and CSV File Tutorial

Read Now →
ā˜…

Laravel 9 PDF | Laravel 9 Generate PDF File using DomPDF

Read Now →
ā˜…

Laravel 9 Vue JS Auth Scaffolding Tutorial

Read Now →
ā˜…

Laravel 9 Eloquent Mutators and Accessors Example

Read Now →
ā˜…

Laravel 9 Image Upload Example Tutorial

Read Now →
ā˜…

Laravel 9 CRUD Application Tutorial Example

Read Now →