Route Resource Controller Laravel

  • 15-05-2022
  • 2065
  • Laravel 9
  • Haresh Chauhan

Laravel route service provider is one of the powers full-service providers to make easy web URLs, If you are familiar with laravel then you knew how to define a web route in web.php file and also know with laravel API how to use in laravel api.php inside the route folder.

In this post you will learn how to use route resources in laravel, it is easy to use especially when you are doing the CRUD method in laravel.

Laravel resource provides a group of routes in laravel, where you did not need to define an individual route in web.php file, route resource method, once you define will cover whole CRUD method in laravel, you just need to define one route for whole crud operation in laravel.

So first understand the definition of resource route in laravel.

What is the resource route in laravel?

Route resource method is the controller of all basic routes required for an application and is easily handled using the resource controller class. If you are using the crud method then just need to make a single route not need to define an easy method route like a store, edit, view, update, delete, the can easy to handle by laravel route resource.

Now we understand how to use the laravel resource route method.

Pre Define Routes In the application.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

/*
|--------------------------------------------------------------------------
| 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::get('user',[UserController::class,'index']);
Route::get('user/create',[UserController::class,'create']);
Route::post('user/',[UserController::class,'store']);
Route::get('user/{id}',[UserController::class,'show']);
Route::get('user/{id}/edit',[UserController::class,'edit']);
Route::patch('user/{id}',[UserController::class,'update']);
Route::delete('user/{id}',[UserController::class,'destroy']);

In the above example, you can see how I had defined the route in laravel, different seven routes define for the whole crud operation.

In the first route to fetch the list of a user for listing, in the second route create or register users form view, in the third route the user creates a post request with a payload that will store in the database, in the fourth route get id user for edit user details, in the fifth route, view the edit form for the user to edit the end switch, and in six routes for used details update in this route payload will come that play load with user details update that will update into the database, In seven route user details used delete method passing params as an id that id user will delete from the database.

Resource Controller

App\Http\Controllers\UserController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
  /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */

  public function index()
  {
    //
  }

  /**
  * Show the form for creating a new resource.
  *
  * @return \Illuminate\Http\Response
  */

  public function create()
  {
    //
  }

  /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */

  public function store(Request $request)
  {
    //
  }

  /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */

  public function show($id)
  {
    //
  }

  /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */

  public function edit($id)
  {
    //
  }

  /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */

  public function update(Request $request, $id)
  {
    //
  }

  /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */

  public function destroy($id)
  {
    //
  }
}

How To Use Route Resource Method?

This one route resource method defines all that seven routes by defining route resource only.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

/*
|--------------------------------------------------------------------------
| 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('user',[UserController::class]);

How To Define Partial Resource Routes

When you define or declare route resource, and you want only a specific route or you may specify a subset of action the controller will handle instead of full action set.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

/*
|--------------------------------------------------------------------------
| 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('user',[UserController::class])->only([
    'index', 'show'
]);

Route::resource('user',[UserController::class])->except([
    'create', 'store', 'update', 'destroy'
]);

If you want to know how it's works then please run below following command.

php artisan route:list

Here below you can see how its work route resource in laravel.

Verb (Methods) URL ACTION ROUTE NAME
GET /user index user.index
GET /user/create create user.create
POST /user store user.store
GET /user/{id} show user.show
GET /user/{id}/edit edit user.edit
PATCH /user/{id} update user.update
DELETE /user/{id} destroy user.destroy

Instead of this, you can define multiple route resources in a single route group, there is no need for other route resources needed.

Multiple Route Resource Define

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\StudentController;

/*
|--------------------------------------------------------------------------
| 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([
  'user',[UserController::class]
  'student',[StudentController::class]
]);

You can easy to use multiple route resources in laravel.

So you can easy to understand how to use route resources in laravel, the above post will easy to understand how to use route resources and resource controllers in laravel.

Thanks for reading our post.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :