Laravel 8 Resource Route Controller Example

In this example we will show laravel 8 resource route controller example. This example will give you simple example of laravel 8 resource route. we will show laravel 8 resource routing example. Today i explain step by step what is resource route in laravel 8.

You can simply understand of resource route and controller in laravel 8 application. 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.

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

First 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:

Read Also :  Google Line Chart Example in Laravel

Laravel 8 Resource Routing Example

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:

Resource Route:

<?php
  
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BlogController;

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

After, 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:

Read Also :  Laravel Dynamic Google Pie Chart Example

Resource Controller Command:

php artisan make:controller BlogController --resource

After 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)
    {
        //
    }
}

I hope you understand of laravel 8 resource route controller and it can help you…

Read Also :  Laravel Carbon Get Yesterday Date Example