DEV Community

Cover image for How to Generate QR Codes in Laravel 11
MK
MK

Posted on • Originally published at techtales.blog on

How to Generate QR Codes in Laravel 11

Creating QR codes in Laravel 11 applications can be an easy process with the right packages and setup. In this post, we will walk through how to generate QR codes using the popular simple-qrcode package, which is a wrapper for the robust BaconQrCode library. We will cover everything from installation to rendering QR codes in your views.

Prerequisites

Before start, make sure you have Laravel 11 installed on your system. If Laravel 11 is released by the time you read this guide, the methods described here should be applicable for Laravel 8 and later, assuming no major changes to package management or service providers.

Step 1: Install the Simple-QRCode Package to generate QR codes

First, you need to add the QR code package to your Laravel project. Run the following command in your terminal:

composer require simplesoftwareio/simple-qrcode
Enter fullscreen mode Exit fullscreen mode

Step 2: Configuration

With Laravel’s auto-discovery feature, the service provider and facade will automatically be registered. However, if you need to manually register it, you can add the provider and alias to your config/app.php file:

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

'aliases' => [
    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
],
Enter fullscreen mode Exit fullscreen mode

Step 3: Generating QR Codes

You can generate QR codes directly in your controllers or views. Here’s how you can generate a basic QR code:

In a Controller Method:
use SimpleSoftwareIO\QrCode\Facades\QrCode;

public function generateQRCode()
{
    $image = QrCode::size(300)->generate('Embed this content into the QR Code');

    return view('qrCode', ['image' => $image]);
}
Enter fullscreen mode Exit fullscreen mode
In a Blade View:

You can directly embed the QR code generation in a Blade template like this:

<div>
    {!! QrCode::size(300)->generate('Embed this content into the QR Code') !!}
</div>
Enter fullscreen mode Exit fullscreen mode

Step 4: Advanced Options

The simple-qrcode package offers various customization options, such as changing the size, color, margin, and adding a logo in the middle of the QR code:

public function generateCustomQRCode()
{
    $image = QrCode::format('png')
                    ->size(300)
                    ->color(255, 0, 0)
                    ->backgroundColor(255, 255, 255)
                    ->margin(1)
                    ->merge('/public/path-to-logo.jpg', .3)
                    ->generate('Customize your QR code content here');

    return view('customQrCode', ['image' => $image]);
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Integrating QR code functionality into your Laravel application is straightforward with the simple-qrcode package. You can use QR codes for a variety of applications such as user authentication, product information, link sharing, and more.

Checkout our other posts about laravel here.

For further customization and advanced features, refer to the official documentation of the simple-qrcode package.

The post How to Generate QR Codes in Laravel 11 appeared first on TechTales.

Top comments (0)