How to Generate Various QR Codes in Laravel 9

We will go through all the necessary steps, to tell you how to generate different QR codes in your Laravel 9 application using the simple QR code package.

A simple QR code generator will give you the freedom to generate various types of QR Codes in your Laravel application. It gives a simple QRCode wrapper, that is easy to integrate in Laravel apps.

It is easy to install this package in Laravel using the Composer package. The package services should be registered in your config/app.php file. Then, you can easily generate a variety of QR codes in your Laravel application like simple QR codes, coloured QR codes and even rapidly increase the QR codes in Laravel with high-end customization.

Simple QR Code Generator – Example

  • Step 1: Create Laravel Project
  • Step 2: Add Database Details
  • Step 3: Install QR Code Package
  • Step 4: Register QR Code Service
  • Step 5: Build Controller
  • Step 6: Add Route
  • Step 7: Generate QR Codes in Blade View
  • Step 8: Run Laravel App

Create Laravel Project

First, open the terminal and use the command given below to install a new Laravel project. Remember to get inside your project folder.

composer create-project --prefer-dist laravel/laravel laravel-demo
cd laravel-demo

Add Database Details

The .env config file helps you to establish a database connection, you may update this file with your database credentials.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password

If you are using the mac operating system and working on macOS, ensure to add UNIX_SOCKET and DB_SOCKET database credentials in your .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Install QR Code Package

Open your command prompt, and type the given command below, to start installing the simplesoftwareio/simple-qrcode package. It quickly helps create many different kinds of QR codes in your Laravel app.

composer require simplesoftwareio/simple-qrcode

Register QR Code Service

You need to register QR code services in your config/app.php file, so open the file and update the providers and alias array using the details given below.

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

Build Controller

In Laravel applications, all business logic is added to the controller class, so create your controller using the given command.

php artisan make:controller QrCodeController

Next, head over to resources/views/QrCodeController.blade.php and add the code given below.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QrCodeController extends Controller
{
    public function index()
    {
      return view('qrcode');
    }
}

Add Route

We have added the function to load our view file in the controller class. Codes in the controller are run by routes. So, let’s define new routes in our routes/web.php file.

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QrCodeController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('/generate-qrcode', [QrCodeController::class, 'index']);

Generate QR Codes in Blade View

We will learn how to use the view file and generate simple and colored QR Codes in Laravel application.

We are now ready to set up a blade view file, so create the blade view file inside the views folder, and then add the code given below in the resources/views/qrcode.blade.php file.

<!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>

Start Application

Finally, use the PHP artisan command below to start your Laravel server. You may use the given URL to view your app.

php artisan serve
http://127.0.0.1:8000/generate-qrcode
Create Different Barcodes in Laravel

Conclusion

This simple QR code tutorial with Laravel is explained with step-by-step example. Hope you understood every instruction shared so far, and will easily create QR codes in your Laravel application without any problem.

We have thoroughly explained the process of building basic QR codes. However, you can go further and also create QR codes for E-mail Address, MeCard, Website URL, Geo Address, Secured URL, Phone Number, Text (SMS), and Wi-Fi. We are sure you will love this package due to its extended options.