Laravel 9 Resource Route and Controller Example

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.

Laravel 9 Resource Route and Controller Example

So, in this example, we will see how to create resource routes and how they work..medrectangle-3-multi-157{border:none!important;display:block!important;float:none;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important}

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 IlluminateSupportFacadesRoute;

use AppHttpControllersItemController;

/*

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

| 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:

.medrectangle-4-multi-155{border:none!important;display:block!important;float:none;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersItemController;

/*

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

| 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:

Laravel 9 Resource Route and Controller Example

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 AppHttpControllers;

use AppModelsItem;

use IlluminateHttpRequest;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index()

{

}

/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{

}

/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

}

/**

* Display the specified resource.

*

* @param AppModelsItem $item

* @return IlluminateHttpResponse

*/

public function show(Item $item)

{

}

/**

* Show the form for editing the specified resource.

*

* @param AppModelsItem $item

* @return IlluminateHttpResponse

*/

public function edit(Item $item)

{

}

/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param AppModelsItem $item

* @return IlluminateHttpResponse

*/

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

{

}

/**

* Remove the specified resource from storage.

*

* @param AppModelsItem $item

* @return IlluminateHttpResponse

*/

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…

Leave a Comment