Laravel 12: How to Generate QR Codes - A Step-by-Step Guide

Hi everyone! If you’re building a Laravel 12 application and want to add QR codes—for links, emails, phone numbers, or even custom designs—you’re in the right place. In this simple guide, I’ll show you how to generate QR codes using the simplesoftwareio/simple-qrcode package.

We’ll cover everything from basic QR codes to advanced ones with colors, images, and more.

Laravel 12: How to Generate QR Codes - A Step-by-Step Tutorial

Prerequisites

Before we begin, ensure you have:

  1. A Laravel 12 project set up.
  2. Composer installed on your system.
  3. Basic knowledge of Laravel routes, controllers, and Blade templates.

If you don’t have a project yet, don’t worry—I’ll guide you through the setup.

Step 1: Install Laravel 12

If you already have a Laravel 12 project, skip this step. Otherwise, create a new project by running:

laravel new example-app

This command sets up a fresh Laravel 12 application named example-app.

Step 2: Install the Simple QR Code Package

To generate QR codes, we’ll use the simplesoftwareio/simple-qrcode package. Open your terminal and run:

composer require simplesoftwareio/simple-qrcode

This package makes it easy to create and customize QR codes in Laravel.

Step 3: Set Up Routes

Let’s create a route to display the QR code. Open the routes/web.php file and add:

<?php

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

Route::get('qr-code', [QRCodeController::class, 'index'])->name('qr.code');

This route points to an index method in a QRCodeController that we’ll create next.

Step 4: Create the QR Code Controller

Run the following command to create a controller:

php artisan make:controller QRCodeController

Now, let’s explore different ways to generate QR codes by updating the app/Http/Controllers/QRCodeController.php file. Below are eight examples, each showing a unique way to create a QR code.

Example 1: Generate a QR Code for a Link

This creates a QR code for a URL, like a website link.

<?php

namespace App\Http\Controllers;

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

class QRCodeController extends Controller
{
    public function index()
    {
        return QrCode::size(300)->generate('https://www.techsolutionstuff.com');
    }
}

Output: A 300x300 pixel QR code that, when scanned, directs to https://www.techsolutionstuff.com.

Example 2: Generate and Save a QR Code

This generates a QR code and saves it as a PNG file in the public/qrcode folder.

Note: Create a qrcode folder in the public directory before running this code.

<?php

namespace App\Http\Controllers;

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

class QRCodeController extends Controller
{
    public function index()
    {
        $path = public_path('qrcode/'.time().'.png');
        QrCode::size(300)->generate('A simple QR code example', $path);
        return response()->download($path);
    }
}

Output: The QR code is saved as a PNG file (e.g., public/qrcode/1234567890.png) and downloaded.

Example 3: Generate a QR Code with Custom Background Color

This adds a custom background color to the QR code (e.g., orange).

<?php

namespace App\Http\Controllers;

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

class QRCodeController extends Controller
{
    public function index()
    {
        return QrCode::size(300)
                     ->backgroundColor(255, 55, 0)
                     ->generate('A simple QR code example');
    }
}

Output: A 300x300 QR code with an orange background.

Example 4: Generate a QR Code with an Embedded Image

This embeds an image (e.g., a logo) in the center of the QR code.

Note: Place an image (e.g., logo.png) in the public/images folder and update the path in the code.

<?php

namespace App\Http\Controllers;

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

class QRCodeController extends Controller
{
    public function index()
    {
        $image = QrCode::format('png')
                       ->merge(public_path('images/logo.png'), 0.5, true)
                       ->size(500)
                       ->errorCorrection('H')
                       ->generate('A simple QR code example');
        return response($image)->header('Content-type', 'image/png');
    }
}

Output: A 500x500 QR code with your logo in the center.

Example 5: Generate a QR Code for an Email

This creates a QR code that opens an email client with predefined details.

<?php

namespace App\Http\Controllers;

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

class QRCodeController extends Controller
{
    public function index()
    {
        return QrCode::size(500)
                     ->email('[email protected]', 'Welcome!', 'This is a test email.');
    }
}

Output: A QR code that, when scanned, opens an email client with the specified recipient, subject, and body.

Example 6: Generate a QR Code for a Phone Number

This creates a QR code that dials a phone number when scanned.

<?php

namespace App\Http\Controllers;

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

class QRCodeController extends Controller
{
    public function index()
    {
        return QrCode::size(300)->phoneNumber('123-456-7890');
    }
}

Output: A QR code that initiates a call to 123-456-7890.

Example 7: Generate a QR Code for an SMS

This creates a QR code that opens a messaging app with a predefined number and message.

<?php

namespace App\Http\Controllers;

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

class QRCodeController extends Controller
{
    public function index()
    {
        return QrCode::size(300)->SMS('123-456-7890', 'Hello, this is a test message!');
    }
}

Output: A QR code that opens a messaging app with the number and message pre-filled.

Example 8: Generate a QR Code in a Blade Template

You can display a QR code directly in a Blade view. Create a file named qrCode.blade.php in resources/views and add:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 12 QR Code Example</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
    <div class="container mt-5 text-center">
        <h3>Laravel 12 QR Code Example</h3>
        <div class="mt-4">
            {!! QrCode::size(200)->generate('Welcome to my website!') !!}
            <p>Scan the QR code above.</p>
        </div>
    </div>
</body>
</html>

Update the QRCodeController.php to return this view:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class QRCodeController extends Controller
{
    public function index()
    {
        return view('qrCode');
    }
}

Output: A webpage displaying a 200x200 QR code that, when scanned, shows Welcome to my website!.

Step 5: Run the Laravel Application

To test your QR codes, start the Laravel server:

php artisan serve

Visit http://localhost:8000/qr-code in your browser to see the QR code. For each example, update the QRCodeController with the desired code and refresh the page. Use a QR code scanner (e.g., on your phone) to test the output.

Conclusion

Generating QR codes in Laravel 12 is a breeze with the simplesoftwareio/simple-qrcode package! Whether you’re creating QR codes for links, emails, phone numbers, or custom designs, this tutorial has shown you how to do it all.

I hope this guide was easy to follow and inspires you to add QR codes to your Laravel projects. Try experimenting with different sizes, colors, or embedded images to make your QR codes stand out.

Frequently Asked Questions (FAQs)

Q1: What types of data can I encode in a QR code?
A: You can encode URLs, text, email addresses, phone numbers, SMS messages, and more. The simple-qrcode package supports various formats via methods like email(), phoneNumber(), and SMS().

Q2: Why is my QR code not displaying in the browser?
A: Ensure you’re returning the QR code with the correct content type (e.g., image/png) for direct image outputs. For Blade templates, check that the QrCode facade is properly used with {!! !!}.

Q3: Can I change the QR code size or color?
A: Yes! Use the size() method to adjust dimensions (e.g., size(500)) and backgroundColor() or color() to change colors (e.g., backgroundColor(255, 55, 0)).

Q4: Why do I get an error when saving the QR code?
A: Make sure the public/qrcode folder exists and has write permissions. Create it manually if needed (mkdir public/qrcode).

Q5: How do I test if my QR code works?
A: Use a QR code scanner app on your smartphone or a browser extension. Scan the QR code to verify it directs to the correct URL, email, or other data.


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS