laravel application for qr code generation

Now days, QR code are essential part of web application. It doesn't matter you are creating E-commerce site, social media, payment site or any other, It helps to provides easy user interface.

In this blog, you will learn how to create QR code for your Laravel application using simplesoftwareio/simple-qrcode Package. This package provides all functionality for almost all kind of QR codes.

You can easily install this package in Laravel using the Composer package. after installation it requires package services to be registered in config/app.php. After that, you can use it as per your requirements.

In this tutorial, you will need to follow these step to create QR code into your application from scratch.

  • Step 1: Create New Application
  • Step 2: Database Configuration
  • Step 3: Install QR Code Package
  • Step 4: Register QR Code Service
  • Step 5: Build Controller
  • Step 6: Creating Route
  • Step 7: Create Blade File
  • Step 8: Running Application

Note : Skip step 1 and 2 if you already have application.

Create New Application

First, open terminal and create new Laravel application using below command :

Database Configuration

Let's configure database for our application. If you have don't have any database then create a new one. After creating database open .env file from root directory of your project. if .env file is missing from project then copy content from .env.example and create file. .env file defines many common environment variables

Install QR Code Package

Let's head back to terminal at root directory of project and enter below command :

    
composer require simplesoftwareio/simple-qrcode
    

It will take some time to install simplesoftwareio/simple-qrcode package. We will use this package to generate QR codes.

Register QR Code Service

Now, we have installed our package but in order to use it, we need to register it to our application. you can do that by registering it into config\app.php file. so open the file and update the providers and alias array with the given below services.

config\app.php

    
<?php

    return [

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

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

Building New Controller for QR Code

Let's create a controller which will handle, user request and pass user input to view. For creating new controller enter below command into your terminal :

    
php artisan make:controller QrCodeController
    

Now let's modify our controller which will take user input and pass it to view. For that make following changes into your controller :

App\Http\Controllers\QrCodeController.php

    
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class QrCodeController extends Controller
{
    public function showQrCode(Request $request)
    {
        $user_name = $request->input('name');
        return view('qr_code', compact('user_name'));
    }
}
    

Adding QR Code Related Routes

Let's create route which will interacts with the QrCodeController and provides a link to view.

routes\web.php

    
<?php

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

Route::get('/', function () {
    return view('welcome');
});

Route::post('/show-qr-code',[QrCodeController::class, 'showQrCode'])->name('qr_code.show');
    

Creating Blade Files or Views

Here, we are planning to display QR Code from user input so we need to 2 views. First will take user input like name and second will display QR code for that name.

We will use default welcome view to get user input. In this blade file, we will remove all content and just add simple form with one input.

resources\view\welcome.blade.php


Let's create new blade file for display qr codes to user. Here, we will bootstrap for styling output.

resources\view\qr_code.blade.php


Running Application

    
php artisan serve
    

open below URL into your browser :

    
http://127.0.0.1:8000
    

Share your thoughts

Ask anything about this examples