Laravel 8 Resource Routing Example
In this tutorial, we will discuss Laravel 8 Resource Routing Example. In this post, we will show the Laravel 8 resource routing example. Laravel resource routing assigns the typical CRUD routes to a controller with a single line of code. You have to create a resource route on Laravel they provide default insert, update, view, and delete routes. and you have to create a resource controller that will provide a method for inserting, updating, viewing, and deleting.
Now, I will generate a controller at app/Http/Controllers/StoreController.php. So, we will see how to create resource routes and how they work.
We have to understand why we create a resource route instead of different routes. we always declare different routes for our crud application like as below:
#routes/web.php use App\Http\Controllers\StoreController; Route::get('store', '[StoreController::class, 'index']'); Route::get('store/create', '[StoreController::class, 'create']'); Route::post('store', '[StoreController::class, 'store']'); Route::get('store/{blog}/edit', '[StoreController::class, 'edit']'); Route::put('store/{blog}', '[StoreController::class, 'update']'); Route::get('store/{blog}', '[StoreController::class, 'show']'); Route::delete('store/{blog}', '[StoreController::class, 'destroy']');
We have to create six routes above for our crud application module. But we can simply create those six routes into one resource by using bellow resource route:
Resource Route:
#routes/web.php use App\Http\Controllers\StoreController; Route::resource('store', StoreController::class);
Now, you can run below command to create route lists:
php artisan route:list --name=store
Now we have output as like bellow route:
+--------+--------------+-------------------+---------------+------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------------+-------------------+---------------+------------------------------------------------+--------------+
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | store | store.index | App\Http\Controllers\StoreController@index | web |
| | POST | store | store.store | App\Http\Controllers\StoreController@store | web |
| | GET|HEAD | store/create | store.create | App\Http\Controllers\StoreController@create | web |
| | GET|HEAD | store/{blog} | store.show | App\Http\Controllers\StoreController@show | web |
| | PUT|PATCH | store/{blog} | store.update | App\Http\Controllers\StoreController@update | web |
| | DELETE | store/{blog} | store.destroy | App\Http\Controllers\StoreController@destroy | web |
| | GET|HEAD | store/{blog}/edit | store.edit | App\Http\Controllers\StoreController@edit | web |
+--------+--------------+-------------------+---------------+------------------------------------------------+--------------+
I will make:controller Artisan command, it will quickly create such a controller:
Also read: laravel 8 crud operation tutorial for beginners
Resource Controller Command:
php artisan make:controller StoreController --resource
Now open StoreController with the following resource method:
<?php
#app/Http/Controllers/StoreController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StoreController 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)
{
//
}
}