image

Laravel QR Generator

1 Jan, 2023 Abdelrahman Etry

In this topic i'm going to talk about how to generate QR code with with Laravel.

A QR code is a type of two-dimensional matrix barcode, invented in 1994, by Japanese company Denso Wave for labelling automobile parts. A barcode is a machine-readable optical image that contains information specific to the labelled item.

in this topic you'll learn how simply generate QR code and add it to your project .


Steps:


First we need to create and configure our application

1- Create a new Laravel project by opening your terminal and running the following command:

composer create-project laravel/laravel laravel-qr


2- Navigate to project's directory:

cd laravel-qr


3-Install simple-qrcode Laravel package to your project:

composer require simplesoftwareio/simple-qrcode


4- After installation process, you need to register it in your Laravel application in the config file inside

config/app.php


5- Locate the 'providers' array and add the following: 

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


6- In the same page locate the 'aliases' array and add the following:

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


Next step we need to setup the transactions and actions


1- we need to create a controller to handle the actions in our applications by running the following command

php artisan make:controller QrCodeController


2- we need to define the main methods for our application in the controller as follows

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

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


3- We need to create a simple view for our application

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Laravel Generate QR Code Examples</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('RemoteStack') !!}
            </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('RemoteStack') !!}
            </div>
        </div>

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


Finally we need to create the routes for our application


1- Navigate to the web.php file located in routes/web.php and add the following routes

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


You can now test your application by running it in your terminal using the following command

php artisan serve
this will be the URL for the view : http://127.0.0.1:8000/generate-qrcode