Now, let’s see example of laravel 8 resource controller. This post will give you simple example of laravel 8 resource route. if you want to see example of resource route in laravel 8 then you are a right place. step by step explain what is resource route in laravel 8.
follow bellow step for what is resource controller in laravel 8.
You can simply understand a concept of resource route and controller in laravel 8 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:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ItemController;
/*
|--------------------------------------------------------------------------
| 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('items', ItemController::class);
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.
#laravel #php #web-development #programming #developer