Laravel 10 Resource Route Controller Example

10-Apr-2023

.

Admin

Laravel 10 Resource Route Controller Example

Hi all,

This Post will give you example of laravel 10 resource route controller example. this example will help you resource controller using command line or manually in laravel 10 app. here you will learn laravel 10 routing example tutorial. you can see how to create a resource route. follow bellow step for laravel 10 routing – laravel 10 routing example tutorial.

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.

I hope It can help you...

#Laravel 10