Laravel 9 Resource Route Controller Example Tutorial
Teams applying “Laravel 9 Resource Route Controller Example Tutorial” in a production project can use the related planning guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi all,
In this article, we will cover how to laravel 9 resource route controller example tutorial. In this tutorial, we will learn how to create a resource route, controller, API resource route, and API resource controller using command line or manually in laravel 9 app. In this post we will give you Laravel 9 Routing – Laravel 9 Routing Example Tutorial. hear for Laravel 9 Routing – Laravel 9 Routing Example Tutorial we will give you details about it.
We will step by step process of how to create first route in laravel and understanding of laravel routing with brief.
Let's start following example:
Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
CRUD Route:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
/*
|--------------------------------------------------------------------------
| 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::controller(ProductController::class)->group(function(){
Route::get('products', 'index')->name('products.index');
Route::post('products', 'store')->name('products.store');
Route::get('products/create', 'create')->name('products.create');
Route::get('products/{product}', 'show')->name('products.show');
Route::put('products/{product}', 'update')->name('products.update');
Route::delete('products/{product}', 'destroy')->name('products.destroy');
Route::get('products/{product}/edit', 'edit')->name('products.edit');
});
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\ProductsController;
/*
|--------------------------------------------------------------------------
| 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('products',ProductsController::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:
Ok, now we have to create resource controller by using bellow command, so let's run bellow command and check ProductController in your app directory:
Resource Controller Command:
php artisan make:controller ProductController --resource --model=Product
After successfully run above command, you can see your ProductController with following resource method, So let's open and see.
app/Http/Controllers/ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController 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\Product $Product
* @return \Illuminate\Http\Response
*/
public function show(Product $Product)
{
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Product $Product
* @return \Illuminate\Http\Response
*/
public function edit(Product $Product)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $Product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $Product)
{
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $Product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $Product)
{
}
}
Ok, this way you can simply use resource route and controller, make quick crud module for your project.
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial