DEV Community

Cover image for How to Generate QR Code in Laravel 8?
Code And Deploy
Code And Deploy

Posted on

How to Generate QR Code in Laravel 8?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-generate-qr-code-in-laravel-8

In my previous post, I'm posted about barcodes now I'm sharing about QR codes in a separate post. We know that QR codes are so important to quicker the sharing of data like website links, app links, product links, secret codes, and many others. So if you have a project in** Laravel that needs to generate a QR code** maybe it could help you.

how-to-generate-qr-code-in-laravel-8

To shortcut our effort thanks to this QR code generator package by milon.

Okay, let's start.

Step 1: Laravel Installation

composer create-project --prefer-dist laravel/laravel laravel-qrcode

Enter fullscreen mode Exit fullscreen mode

Then go to the project directory:

cd laravel-qrcode
Enter fullscreen mode Exit fullscreen mode

Step 2: Database Configuration

If your Laravel project is fresh then you need to update your database credentials. Just open the .env file in your Laravel 8 project.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name_here
DB_USERNAME=your_database_username_here
DB_PASSWORD=your_database_password_here

Enter fullscreen mode Exit fullscreen mode

Step 3: Install Package

Next, we will install our barcode package by milon. Run the following command below:

composer require milon/barcode
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Controller & Routes

Then run the following command to generate controller:

php artisan make:controller QRCodeController
Enter fullscreen mode Exit fullscreen mode

And add the route below to routes\web.php

Route::get('/qrcode', 'App\Http\Controllers\QRCodeController@index')->name('home.index');
Enter fullscreen mode Exit fullscreen mode

Then see below our QRCodeControllercode:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class QRCodeController extends Controller
{
    public function index() 
    {
        $link = "https://codeanddeploy.com/category/laravel";

        return view('barcode', [
            'link' => $link
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Add View

Then create a file barcode.blade.php to resources/views directory. See the following code below:

<!DOCTYPE html>
    <html>

    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Laravel 8 QR Code Demo - codeanddeploy.com</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
    </head>

    <body>
        <div class="container mt-5">

            <h3>SCAN QR CODE FOR MORE LARAVEL TUTORIALS.</h3>

            <br><br>

            <div class="mb-3">{!! DNS2D::getBarcodeHTML("$link", 'QRCODE') !!}</div>
        </div> 
        </div>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Then run your Laravel project:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Then view the below URL to your browser:

http://127.0.0.1:8000/qrcode
Enter fullscreen mode Exit fullscreen mode

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-generate-qr-code-in-laravel-8 if you want to download this code.

Happy coding :)

Top comments (0)