How to Generate QR Code in Laravel 11?

By Hardik Savani November 12, 2024 Category : Laravel

In this post, I will show you how to generate a QR code in a 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.

laravel 11 qr code generate

Step for Laravel 11 Generate QR Code Example

  • Step 1: Install Laravel 11
  • Step 2: Install simplesoftwareio/simple-qrcode
  • Step 3: Create Route
  • 1: Laravel Generate QR Code for Link Example
  • 2: Laravel Generate QR Code and Save Example
  • 3: Laravel Generate QR Code with Color Example
  • 4: Laravel Generate QR Code with Image Example
  • 5: Laravel Generate Email QR Code Example
  • 6: Laravel Generate Phone QR Code Example
  • 7: Laravel Generate SMS QR Code Example
  • 8: Laravel Generate QR Code in Blade File Example

Let's see the below steps, and you can generate QR code in your Laravel 11 projects as well.

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 example-app

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.itsolutionstuff.com');
    }
}

Output:

2: Laravel Generate QR Code and Save Example

We will use the `size()` and `generate()` methods to create a QR code and save it. You need to create a "qrcode" folder in the public folder. 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)
    {
        $path = public_path('qrcode/'.time().'.png');
  
        return QrCode::size(300)
                     ->generate('A simple example of QR code', $path);
    }
}

3: 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:

4: Laravel Generate QR Code with Image Example

We will use the `size()`, `generate()`, and `merge()` methods to create a QR code with an image. You need to provide the path of the image. 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)
    {
        $image = QrCode::format('png')
                         ->merge(public_path('images/YOUR_IMAGE_NAME.png'), 0.5, true)
                         ->size(500)
                         ->errorCorrection('H')
                         ->generate('A simple example of QR code!');
  
        return response($image)->header('Content-type','image/png');
    }
}

Output:

5: 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('hardik@itsolutionstuff.com', 'Welcome to ItSolutionStuff.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>

Output:

I hope it can help you...

Shares