Laravel 11 Resource Route and Controller Example
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.
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...
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 11 Autocomplete Search from Database Example
- Laravel 11 JQuery UI Ajax Autocomplete Search Example
- Laravel 11 Generate Fake Data using Factory Tinker Example
- How to Get Last Executed Query in Laravel 11?
- Laravel 11 Flash Message Example Tutorial
- Laravel 11 Get Client IP Address Example
- Laravel 11 Select2 Ajax Autocomplete Search Example
- Laravel 11 Send Email using Queue Example
- Laravel 11 REST API Authentication using Sanctum Tutorial
- How to Create, Run & Rollback Migration in Laravel 11?
- Laravel 11 Image Upload Example Tutorial
- Laravel 11 CRUD Application Example Tutorial
- How to Customize Default Middleware in Laravel 11?
- How to Create and Use Traits in Laravel 11?