Laravel 10 Eloquent Resources Route and Controller Example

  Feb 2024
  ITSolutionsGuides
  Category: Laravel
Laravel 10 Eloquent Resources Route and Controller Example

Welcome To ITSolutionsGuides,

Resource routes in Laravel 10 offer a concise and RESTful way to define multiple routes for CRUD operations on a resource. With just a single line of code, you can declare routes for all standard CRUD operations (Create, Read, Update, Delete) associated with a particular resource. This not only reduces the amount of code but also ensures a consistent and predictable URL structure for your application's endpoints. In Laravel 10, a resource controller is a PHP class that handles HTTP requests related to a resource. By defining methods for each CRUD operation, you can encapsulate the logic for handling these requests within a single controller, promoting code reusability and maintainability. In this example, we've declared a resource route for a hypothetical "posts" resource and generated a corresponding resource controller using Artisan. Now, the ItemController class will contain methods like index(), create(), store(), show(), edit(), update(), and destroy(), each responsible for handling a specific CRUD operation.

Lets see how to use Laravel 10's Eloquent resources with a comprehensive route and controller with example.

Let's Create Resource Controller

The below artisan command will create the resource controller using the --resource flag. And also to create the model we will use the flag --model=Item in the name of Item.php,

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

Let's Create Resource Controller

The above artisan command will create the controller with all the functions required for the CRUD operations,


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
    {
           
    }
}

Let's Create Route

In the route file we will also declare it as a resource controller, Since it was a resource controller we no need to add all the CRUD routes in the routes file,


routes/web.php

<?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);

Let's Start Coding

We can use the below artisan command to view all the routes ( hidden routes with resource ) ,

php artisan route:list

the above command will display al the routes in the command line,


The actual route file will look like this without the resource controller,

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

});

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides