Laravel 7/6 Resource Route and Controller Tutorial
I know you have lots of question like what is resource route in laravel 7/6?, what is resource controller in laravel 7/6?, how to use resource route in laravel 7/6?, how to use resource controller in laravel 7/6?, I will explain you about laravel 7/6 resource route and resource controller.
You can simply understand a concept of resource route and controller in laravel 7 application. you need to just follow this tutorial.
Laravel resource controller and resource route is pretty interesting feature to create quick CRUD application in laravel. For resource you have to do two things on laravel application. first you have to create resource route on laravel they provide insert, update, view, delete routes and second you have to create resource controller that will provide method for insert, update, view and delete.
So, in this example we will see how to create resource route and how they work.
First we have to understand why we choose resource route instead of different route. we always declare different 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 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 |
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
Ok, now we have to create resource controller by using bellow command, so let's run bellow command and check ItemController in your app directory:
Resource Controller Command:
php artisan make:controller ItemController --resource --model=Item
After successfully run above command, you can see your ItemController with following resource method, So let's open and see.
app/Http/Controllers/ItemController.php
<?php
namespace 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 quick crud module for your project.
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 8/7/6 Google ReCAPTCHA Form Validation Example
- Send Email with Laravel 7/6 using Markdown Mailable Class
- Laravel 7/6 Dropzone Image Upload Tutorial
- Laravel 7/6 REST API with Passport Tutorial
- Laravel 6 Email Verification Tutorial
- Laravel 7/6 Multi Auth (Authentication) Tutorial
- Laravel 6 CORS Middleware Tutorial
- Laravel 6 Authentication Tutorial
- Laravel 6 CRUD Application Tutorial
- How to Create Resource Controller in Laravel?