In this tutorial, we will see how to create resource route in laravel 8. We will see laravel 8 resource routing example.

We always declare different route for our crud application as shown below:

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']');


Now, register a resourceful route to the controller

Resource Route:

routes/web.php

use App\Http\Controllers\BlogController;
Route::resource('blogs', BlogController::class);

Now run the command below:

php artisan route:list --name=blogs

We will get output like this:

+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
| 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          |

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

Resource Controller Command:
php artisan make:controller BlogController --resource


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 route and controller


I hope this example helps you.