How To Generate QR Code Using Laravel

Hello Geek, QR codes are sometimes required for product identification, inventory, and other purposes. So, how do you make a QRcode? It’s simple; we can use Simple QRcode to generate QR codes in the Laravel framework.

We’ve all seen how popular QR codes have become. A QR code is simply an encrypted image of some unreadable content. It must make use of some QR code readers.

In this article, we will learn how to create or generate a QRcode so that when we scan it, we can be directed to SMS, email, a website, or simply find out what data is hidden behind the QR code.

In this article, we will begin from scratch by creating a new Laravel project.

This guide will walk you through all of the steps required to generate various QR codes in the Laravel application using the simple QR code package.

In the Laravel app, a simple QR code generator allows you to generate various types of QR Codes. It provides a simple QrCode wrapper that is simple to integrate into Laravel.

How To Generate QR Code Using Laravel

To begin, open Terminal and enter the following command to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel:^9.0 qr-code-example

or, if you have installed the Laravel Installer as a global composer dependency:

laravel new qr-code-example

Add Database Details
After, Installation Go to the project’s root directory, open the.env file, and configure the database as follows:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=<DATABASE NAME>
DB_USERNAME=<DATABASE USERNAME>
DB_PASSWORD=<DATABASE PASSWORD>

Install QR Code Package

composer require simplesoftwareio/simple-qrcode

Register QR Code Service

<?php

    return [

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

Create Controller

php artisan make:controller QrCodeController

Next, Open QrCodeController.php and add the following code into the file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

Add Route

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QrCodeController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

Route::get('/qrcode', [QrCodeController::class, 'index']);

Generate QR Codes in Blade View

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>How to Generate QR Code in Laravel 9</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
</head>

<body>

    <div class="container mt-4">

        <div class="card">
            <div class="card-header">
                <h2>Simple QR Code</h2>
            </div>
            <div class="card-body">
                {!! QrCode::size(300)->generate('https://techvblogs.com/blog/generate-qr-code-laravel-9') !!}
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h2>Color QR Code</h2>
            </div>
            <div class="card-body">
                {!! QrCode::size(300)->backgroundColor(255,90,0)->generate('https://techvblogs.com/blog/generate-qr-code-laravel-9') !!}
            </div>
        </div>

    </div>
</body>
</html>

Helpers For Generate Different QR Codes

BitCoin

This helper generates a scannable bitcoin to send payments.

QrCode::BTC($address, $amount);

//Sends a 0.334BTC payment to the address
QrCode::BTC('bitcoin address', 0.334);

//Sends a 0.334BTC payment to the address with some optional arguments
QrCode::size(500)->BTC('address', 0.0034, [
    'label' => 'my label',
    'message' => 'my message',
    'returnAddress' => 'https://www.returnaddress.com'
]);

E-Mail

This helper generates an e-mail QRcode that is able to fill in the e-mail address, subject, and body:

QrCode::email($to, $subject, $body);

//Fills in the to address
QrCode::email('foo@bar.com');

//Fills in the to address, subject, and body of an e-mail.
QrCode::email('foo@bar.com', 'This is the subject.', 'This is the message body.');

//Fills in just the subject and body of an e-mail.
QrCode::email(null, 'This is the subject.', 'This is the message body.');

Geo

This helper generates latitude and longitude that a phone can read and opens the location in Google Maps or a similar app.

QrCode::geo($latitude, $longitude);

QrCode::geo(37.822214, -122.481769);

Phone Number

This helper generates a QR code that can be scanned and then dials a number.

QrCode::phoneNumber($phoneNumber);

QrCode::phoneNumber('555-555-5555');
QrCode::phoneNumber('1-800-Laravel');

SMS (Text Messages)

This helper makes SMS messages that can be prefilled with the send-to address and body of the message:

QrCode::SMS($phoneNumber, $message);

//Creates a text message with the number filled in.
QrCode::SMS('555-555-5555');

//Creates a text message with the number and message filled in.
QrCode::SMS('555-555-5555', 'Body of the message');

WiFi

This helper makes scannable QR codes that can connect a phone to a WiFi network:

QrCode::wiFi([
    'encryption' => 'WPA/WEP',
    'ssid' => 'SSID of the network',
    'password' => 'Password of the network',
    'hidden' => 'Whether the network is a hidden SSID or not.'
]);

//Connects to an open WiFi network.
QrCode::wiFi([
    'ssid' => 'Network Name',
]);

//Connects to an open, hidden WiFi network.
QrCode::wiFi([
    'ssid' => 'Network Name',
    'hidden' => 'true'
]);

//Connects to a secured WiFi network.
QrCode::wiFi([
    'ssid' => 'Network Name',
    'encryption' => 'WPA',
    'password' => 'myPassword'
]);

Run Laravel App

php artisan serve

All the best nerd!

Leave a Reply