In this article, I will explain the way how to implement the Laravel 9 resource controller. I would like to give you a Laravel 9 resource route declaration example. We are going to learn you how to use resource controllers in Laravel 9. I would like to show you the resource route in Laravel 9.

You know that the resource route reduces the number of routes and makes your route file clear. So if you have a crud part in your Laravel application, you can use the resource route and controller. I will recommend you for that.

We can define a country route for a crud operation like:

<?php

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

Route::controller(CountryController::class)->group(function(){
  Route::get('country', 'index')->name('country.index');
  Route::post('country', 'store')->name('country.store');
  Route::get('country/create', 'create')->name('country.create');
  Route::get('country/{item}', 'show')->name('country.show');
  Route::get('country/{item}/edit', 'edit')->name('country.edit');
  Route::put('country/{item}', 'update')->name('country.update');
  Route::delete('country/{item}', 'destroy')->name('country.destroy');
});

 

Now we can define those routes below to do the same job:

<?php
  
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CountryController;
   
/*
|--------------------------------------------------------------------------
| 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', CountryController::class);

 

Now if you run the below command php artisan route:list you will see the route name, route request, and uri also. 

laravel-resource-controller-route-list

Resource Controller Command

Ok, now we need to create a resource controller by using the bellow command, so let's run the bellow command and check CountryController in your app directory:

php artisan make:controller CountryController --resource --model=Country

 

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/CountryController.php

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

 

Read also: Laravel 9 CRUD Tutorial Example Using Eloquent ORM

 

Hope it can help you.

Category : #laravel

Tags : #laravel , #laravel routing