Raviya Technical
Published in

Raviya Technical

Laravel Basic | Laravel 7/6 Resource Route and Controller Tutorial

I know you have lots of questions like what is the resource route in laravel 7/6? what is the resource controller in laravel 7/6? how to use the resource route in laravel 7/6? how to use resource controller in laravel 7/6? I will explain to you about the laravel 7/6 resource route and resource controller.

You can simply understand the concept of resource route and controller in the laravel 7 application. you need to just follow this tutorial.

Laravel resource controller and resource route is a pretty interesting feature to create a quick CRUD application in laravel. For resources, you have to do two things on the laravel application. first, you have to create a resource route on laravel they provide insert, update, view, delete routes and second, you have to create a resource controller that will provide method for insert, update, view, and delete.

So, in this example, we will see how to create resource routes and how they work.

First, we have to understand why we choose the resource route instead of a different route. we always declare different route for our crud application like as bellow:

CRUD Route:

Route::get('items',['as'=>'items.index','uses'=>'ItemController@index']);Route::post('items/create',['as'=>'items.store','uses'=>'ItemController@store']);Route::get('items/edit/{id}',['as'=>'items.edit','uses'=>'ItemController@edit']);Route::patch('items/{id}',['as'=>'items.update','uses'=>'ItemController@update']);Route::delete('items/{id}',['as'=>'items.destroy','uses'=>'ItemController@destroy']);Route::get('items/{id}',['as'=>'items.view','uses'=>'ItemController@view']);

As you can see above route declare, we have to create six routes for our crud application module. But we can simply create those six routes by using bellow resource route:

Resource Route:

Route::resource('items', 'ItemController');

Now, you can run bellow command and check to create route lists:

php artisan route:list

Now we have output as like bellow created route:

+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+| Domain | Method    | URI               | Name          | Action                                      | Middleware   |+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+|        | GET|HEAD  | api/user          |               | Closure                                     | api,auth:api ||        | GET|HEAD  | items             | items.index   | App\Http\Controllers\ItemController@index   | web          ||        | POST      | items             | items.store   | App\Http\Controllers\ItemController@store   | web          ||        | GET|HEAD  | items/create      | items.create  | App\Http\Controllers\ItemController@create  | web          ||        | GET|HEAD  | items/{item}      | items.show    | App\Http\Controllers\ItemController@show    | web          ||        | PUT|PATCH | items/{item}      | items.update  | App\Http\Controllers\ItemController@update  | web          ||        | DELETE    | items/{item}      | items.destroy | App\Http\Controllers\ItemController@destroy | web          ||        | GET|HEAD  | items/{item}/edit | items.edit    | App\Http\Controllers\ItemController@edit    | web          |+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

Also, Use Route Package

Ok, now we have to create a resource controller by using the below command, so let’s run below command and check ItemController in your app directory:

Resource Controller Command:

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

After successfully running the above command, you can see your ItemController with the following resource method, So let’s open and see.

app/Http/Controllers/ItemController.php

<?phpnamespace App\Http\Controllers;use App\Item;use Illuminate\Http\Request;class ItemController 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  \App\Item  $item* @return \Illuminate\Http\Response*/public function show(Item $item){}/*** Show the form for editing the specified resource.** @param  \App\Item  $item* @return \Illuminate\Http\Response*/public function edit(Item $item){}/*** Update the specified resource in storage.** @param  \Illuminate\Http\Request  $request* @param  \App\Item  $item* @return \Illuminate\Http\Response*/public function update(Request $request, Item $item){}/*** Remove the specified resource from storage.** @param  \App\Item  $item* @return \Illuminate\Http\Response*/public function destroy(Item $item){}}

Ok, this way you can simply use resource route and controller, make a quick crud module for your project.

I hope It can help you…

--

--

--

PHP, Laravel And Flutter Development

Recommended from Medium

Open Source Will Play a Larger Role in the Digital Government of the Future

A World More Embracing 2020

The Spiderwiz Vision — Decentralizing the Internet

Elixir for the Little Things

At-Least-Once delivery using the Azure Cosmos DB Change Feed Processor

Variations of LIS

Docker 101: Remove “sudo” need from command on Ubuntu

Running Pi-hole in a container on my Synology server to block ads everywhere in my home network on…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raviya Technical

Raviya Technical

raviyatechnical.blogspot.com

More from Medium

Laravel Basic | Laravel 6 Form Validation with Error Messages

Laravel Advance | How to Add Index in Laravel Migration

Laravel Basic | Laravel File Upload Tutorial Example

Laravel Basic | Laravel Ajax Image Upload Example