Laravel 11 Resource Route and Controller Example

By Hardik Savani April 16, 2024 Category : Laravel

In this short article, we will learn how to create resource routes and controller in laravel 11 application.

we know what is controller and route in laravel and how to use it. but here i will show you how to use resource route and controller in laravel 11.

laravel 11 resource controller

What is Resource Route in Laravel 11?

Routes in Laravel define the endpoints of your application. Laravel provides a powerful routing system that allows you to map HTTP request methods and URIs to specific controller actions or closures.

When working with resourceful controllers, Laravel provides a convenient way to define routes for all CRUD operations using the Route::resource() method. This method automatically generates the necessary routes for the resource controller based on RESTful conventions.

Here's how you define resourceful routes for the PostController in Laravel:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
Route::resource('posts', App\Http\Controllers\PostController::class);

This single line of code will generate the following routes:

+-----------+----------------------------------------+----------------------+---------------------------+
| Method    | URI                                    | Action               | Route Name                |
+-----------+----------------------------------------+----------------------+---------------------------+
| GET       | /posts                                 | index                | posts.index               |
| GET       | /posts/create                          | create               | posts.create              |
| POST      | /posts                                 | store                | posts.store               |
| GET       | /posts/{post}                          | show                 | posts.show                |
| GET       | /posts/{post}/edit                     | edit                 | posts.edit                |
| PUT/PATCH | /posts/{post}                          | update               | posts.update              |
| DELETE    | /posts/{post}                          | destroy              | posts.destroy             |
+-----------+----------------------------------------+----------------------+---------------------------+

These routes map to the corresponding methods in the PostController to handle CRUD operations for the Post resource.

What is Resource Controller in Laravel 11?

A resource controller is a type of controller in Laravel that provides a convenient way to organize the logic for handling CRUD operations related to a particular resource, such as articles, users, products, etc.

Typically, a resource controller will contain methods to handle various HTTP verbs (GET, POST, PUT/PATCH, DELETE) for CRUD operations. These methods include index() for listing all resources, create() for displaying a form to create a new resource, store() for storing a newly created resource, show() for displaying a specific resource, edit() for displaying a form to edit a resource, update() for updating a specific resource, and destroy() for deleting a specific resource.

Here's an example of a resource controller definition in Laravel:

You can generate resource controller by using the following commands:

php artisan make:controller PostController --resource

You can also specify modal name:

php artisan make:controller PostController --resource --model=Post

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

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(Post $post)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Post $post)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Post $post)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Post $post)
    {
        //
    }
}

Resource controllers and routes provide a clean and organized way to manage CRUD operations in Laravel applications, following RESTful principles.

I hope It can help you...

Shares