How to generate QR code in Laravel 8

How to generate QR code in Laravel 8

Last Updated: 30 Nov, 2022

What is QR code?

A QR (QUICK RESPONSE) code is a type of two-dimensional barcode. It is invented in 1994 by the Japanese automotive company Denso Wave. A barcode is a machine-readable optical label that contains information about the item to which it is attached. QR codes are now used in a much broader context, including both commercial tracking applications and convenience-oriented applications aimed at mobile-phone users (termed mobile tagging). In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application.

Now, lets go through step by step and see how we can generate QR code in Laravel 8.

Step 1: Create fresh laravel 8 application

Open terminal and install fresh laravel application using below command:

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

Next, lets get inside the qrapp using below command:

cd qrcodeapp

Step 2: Install QR code laravel package

Next, we need to install simple-qrcode package for QR code generator. Type below command in terminal:

composer require simplesoftwareio/simple-qrcode

Next, open config/app.php file and add service provider and aliase.

<?php
return [
    'providers' => [
        ....                
        SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
    ],

    'aliases' => [
        ....
        'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
    ]
];

Step 3: Create route

Next, we will create a route to test our example code. Open routes/web.php file and add below route in it:

<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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::get('/qrcode', 'QRCodeController@'index');

Step 4: Create controller

Next, create QRCodeController.php file and put below code in it:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class QRCodeController extends Controller
{
    public function index()
    {
          return view('qrcode');
    }
}

Step 5: Create balde file

Next, create qrcode.blade.php file and put below code in it:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Generate QR Code in Laravel 8</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
    <div class="container mt-3">
        <div class="row">
            <div class="col-12 col-sm-6 col-md-4 mb-3">
                <div class="card">
                    <div class="card-header">
                        <h5 class="text-center">Simple QR Code</h5>
                    </div>
                    <div class="card-body text-center">
                        {!! QrCode::size(300)->generate('W3TechPoint') !!}
                    </div>
                </div>
            </div>
            <div class="col-12 col-sm-6 col-md-4 mb-3">
                <div class="card">
                    <div class="card-header">
                        <h5 class="text-center">Simple QR Code</h5>
                    </div>
                    <div class="card-body text-center">
                        {!! QrCode::size(300)->generate('w3techpoint@gmail.com') !!}
                    </div>
                </div>
            </div>
            <div class="col-12 col-sm-6 col-md-4 mb-3">
                <div class="card">
                    <div class="card-header">
                        <h5 class="text-center">Simple QR Code</h5>
                    </div>
                    <div class="card-body text-center">
                        {!! QrCode::size(300)->generate('Happy learning laravel 8') !!}
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-12 col-sm-6 col-md-4 mb-3">
                <div class="card">
                    <div class="card-header">
                        <h5 class="text-center">Coloured QR Code</h5>
                    </div>
                    <div class="card-body text-center">
                        {!! QrCode::size(300)->backgroundColor(255,100,0)->generate('W3TechPoint') !!}
                    </div>
                </div>
            </div>
            <div class="col-12 col-sm-6 col-md-4 mb-3">
                <div class="card">
                    <div class="card-header">
                        <h5 class="text-center">Coloured QR Code</h5>
                    </div>
                    <div class="card-body text-center">
                        {!! QrCode::size(300)->backgroundColor(100,255,0)->generate('W3TechPoint') !!}
                    </div>
                </div>
            </div>
            <div class="col-12 col-sm-6 col-md-4 mb-3">
                <div class="card">
                    <div class="card-header">
                        <h5 class="text-center">Coloured QR Code</h5>
                    </div>
                    <div class="card-body text-center">
                        {!! QrCode::size(300)->backgroundColor(255,255,0)->generate('W3TechPoint') !!}
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Step 6: Start the development server using below command:

php artisan serve

Next, open below url in browser and see the result:

http://127.0.0.1:8000/qrcode

HAPPY LEARNING :)

 

Thank You, Please Share.