How to Generate QR Code in Laravel 11

How to Generate QR Code in Laravel 11?

In this post, I will show you How to Generate QR Code in Laravel 11 application. We will generate and save the QR code for a link.

We will use the simplesoftwareio/simple-qrcode composer package to generate QR codes in Laravel 11. simplesoftwareio/simple-qrcode provides methods to generate QR codes, save QR codes, generate QR codes for link, generate QR codes for phone numbers, generate QR codes for emails, and generate QR codes with downloads. So, we will see one-by-one example code. You Can Learn How to Generate Barcode in Laravel 11?

Let’s see the below steps How to Generate QR Code in Laravel 11?

Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel Generate-QR-Code
cd Generate-QR-Code

Install simplesoftwareio/simple-qrcode

In the first step, we will install the simplesoftwareio/simple-qrcode Package that provides the capability to generate QR codes in a Laravel application. So, first open your terminal and run the below command:

composer require simplesoftwareio/simple-qrcode

Create Route

In this step, we will add routes and a controller file. So first, add the below route in your routes.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\QRCodeController;
  
Route::get('qr-code', [QRCodeController::class, 'index']);

1: Laravel Generate QR Code for Link Example

We will use the `size()` and `generate()` methods to create a QR code for the link. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::size(300)->generate('https://www.DevScriptSchool.com');
    }
}

Output:

How to Generate QR Code in Laravel 11

2: Laravel Generate QR Code with Color Example

We will use the `size()`, `generate()` and `backgroundColor()` methods to create a QR code with background color. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::size(300)
                     ->backgroundColor(255,55,0)
                     ->generate('A simple example of QR code');
    }
}

Output:

How to Generate QR Code in Laravel 11

4: Laravel Generate Email QR Code Example

We will use the `size()`, and `email()` methods to create a QR code with for email. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::size(500)
                 ->email('support@DevScriptSchool.com', 'Welcome to DevScriptSchool.com!', 'This is !.');
    }
}

6: Laravel Generate Phone QR Code Example

We will use the `size()`, and `phoneNumber()` methods to create a QR code with for phone number. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::phoneNumber('111-222-6666');
    }
}

7: Laravel Generate SMS QR Code Example

We will use the `size()`, and `SMS()` methods to create a QR code with for sending sms. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        QrCode::SMS('111-222-6666', 'Body of the message');
    }
}

8: Laravel Generate QR Code in Blade File Example

Here, You can generate OR code in blade file:

Code:

<div class="visible-print text-center">
    {!! QrCode::size(100)->generate('Demo'); !!}
    <p>Scan me to return to the original page.</p>
</div>

I hope it can help you…

Leave a Reply