Laravel 10 Resource Route and Controller Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Guzzle Http Request Example
- Laravel 10 Change Date Format Examples
- Laravel 10 Yajra Datatables Tutorial Example
- Laravel 10 Markdown | Laravel 10 Send Email using Markdown Mailables
- Laravel 10 REST API Authentication using Sanctum Tutorial
- Laravel 10 Ajax Image Upload Example
- Laravel 10 Authentication using Jetstream Tutorial
- Laravel 10 Import Export Excel and CSV File Tutorial
- Laravel 10 Authentication using Breeze Tutorial
- Laravel 10 Generate PDF File using DomPDF Example
- Laravel 10 Multiple Image Upload Tutorial Example
- Laravel 10 Image Upload Example Tutorial
- Laravel 10 CRUD Application Example Tutorial