Laravel 8 Resource Routing Example Tutorial

Laravel 6 , Laravel , Laravel 7 , Laravel 8

Nicesnippets

9785

26-09-2020


Laravel 8 Resource Routing Example Tutorial

Hi Guys,

Today,I will explain you how to create resource route in laravel 8. we will show laravel 8 resource routing example.laravel resource routing assigns the typical "crud" routes to a controller with a single line of code. for example, you may wish to create a controller that handles all http requests for "blogs" stored by your application. using the make:controller artisan command, you can quickly create such a controller.

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.

Here ,I will generate a controller at app/Http/Controllers/BlogController.php. The controller will contain a method for each of the available resource operations.

So, in this example we will see how to create resource route and how they work.

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:

use App\Http\Controllers\BlogController;

Route::get('blogs', '[BlogController::class, 'index']');

Route::get('blogs/create', '[BlogController::class, 'create']');

Route::post('blogs', '[BlogController::class, 'store']');

Route::get('blogs/{blog}/edit', '[BlogController::class, 'edit']');

Route::put('blogs/{blog}', '[BlogController::class, 'update']');

Route::get('blogs/{blog}', '[BlogController::class, 'show']');

Route::delete('blogs/{blog}', '[BlogController::class, 'destroy']');

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:

Here, you may register a resourceful route to the controller:

Resource Route:

routes/web.php

use App\Http\Controllers\BlogController;

Route::resource('blogs', BlogController::class);

Now, you can run bellow command and check create route lists:

php artisan route:list --name=blogs

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 | blogs | blogs.index | App\Http\Controllers\BlogController@index | web |

| | POST | blogs | blogs.store | App\Http\Controllers\BlogController@store | web |

| | GET|HEAD | blogs/create | blogs.create | App\Http\Controllers\BlogController@create | web |

| | GET|HEAD | blogs/{blog} | blogs.show | App\Http\Controllers\BlogController@show | web |

| | PUT|PATCH | blogs/{blog} | blogs.update | App\Http\Controllers\BlogController@update | web |

| | DELETE | blogs/{blog} | blogs.destroy | App\Http\Controllers\BlogController@destroy | web |

| | GET|HEAD | blogs/{blog}/edit | blogs.edit | App\Http\Controllers\BlogController@edit | web |

+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

I will make:controller Artisan command, we can quickly create such a controller:

Resource Controller Command:

php artisan make:controller BlogController --resource

Here you can see your BlogController with following resource method, So let's open and see.

app/Http/Controllers/BlogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BlogController 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 int $id

* @return \Illuminate\Http\Response

*/

public function show($id)

{

//

}

/**

* Show the form for editing the specified resource.

*

* @param int $id

* @return \Illuminate\Http\Response

*/

public function edit($id)

{

//

}

/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param int $id

* @return \Illuminate\Http\Response

*/

public function update(Request $request, $id)

{

//

}

/**

* Remove the specified resource from storage.

*

* @param int $id

* @return \Illuminate\Http\Response

*/

public function destroy($id)

{

//

}

}

this way you can simply use resource route and controller, make quick crud module for your project.

It Will help you...


Recommended Posts