Loading image

Blogs / Programming

Complete Guide to Generating and Storing QR Codes in Laravel 11

Complete Guide to Generating and Storing QR Codes in Laravel 11

  • showkat ali
  • 0 Comments
  • 180 View

QR codes are widely used for encoding information in a visually scannable format. This guide explains how to generate QR codes in Laravel, store them in a database, and resolve the common issue: You need to install the imagick extension to use this back end.


Step 1: Install the Simple QR Code Package

Laravel provides a package for generating QR codes called simple-qrcodecalled. Install it using Composer:

composer require simplesoftwareio/simple-qrcode

This package uses the BaconQrCode library for rendering QR codes and supports various formats like SVG and PNG.


Step 2: Setting Up the Database

To store the QR code in the database, ensure your table has a column for it.

Create a Migration

Run the following command to create a migration:

php artisan make:migration add_qr_code_to_bookings_table --table=bookings

In the migration file, add a column to store the QR code:

public function up()
{
    Schema::table('bookings', function (Blueprint $table) {
        $table->longText('qr_code')->nullable(); // To store QR code as SVG or binary
    });
}

public function down()
{
    Schema::table('bookings', function (Blueprint $table) {
        $table->dropColumn('qr_code');
    });
}

Run the migration:

php artisan migrate

Step 3: Generating and Storing QR Codes

Use the QrCode facade to generate QR codes and store them in the database.

Controller Code

Here is an example of how to generate a QR code for a booking and store it:

use SimpleSoftwareIO\QrCode\Facades\QrCode;
use App\Models\Booking;

public function generateAndStoreQrCode($id)
{
    // Find the booking by ID
    $booking = Booking::findOrFail($id);

    // Generate the QR code as SVG
    $hashedIdentifier = $booking->reference_code; // Replace with your unique data
    $qrCode = QrCode::size(300)->generate($hashedIdentifier);

    // Store the QR code in the database
    $booking->qr_code = $qrCode;
    $booking->save();

    return response()->json([
        'message' => 'QR Code generated and saved successfully.',
        'booking' => $booking,
    ]);
}

Step 4: Displaying the QR Code

You can display the QR code in your Blade view using the {!! !!} directive to render the raw SVG content.

Blade Template

Create a view file, such as resources/views/booking/qr-code.blade.php:

 
<!DOCTYPE html>
<html>
<head>
    <title>Booking QR Code</title>
</head>
<body>
    <h1>Your Booking QR Code</h1>
    {!! $booking->qr_code !!}
</body>
</html>
 

Controller for Displaying QR Code

public function showBooking($id)
{
    $booking = Booking::findOrFail($id);
    return view('booking.qr-code', compact('booking'));
}

Step 5: Understanding the Imagick Issue

The error You need to install the imagick extension to use this back end occurs because the simple-qrcode package defaults to the Imagick backend when generating QR codes in PNG format. This backend requires the imagick PHP extension, which may not be installed or configured on your server.

What is Imagick?

Imagick is a PHP extension that provides a wrapper for the ImageMagick library, enabling advanced image manipulation.

Why Does the Error Occur?

When generating QR codes in formats like PNG (format('png')), the package attempts to use Imagick for rendering. If Imagick is not installed, you encounter this error.

How to Fix It

Option 1: Install the Imagick Extension

If you prefer to use the PNG format, install the Imagick extension:

For Ubuntu/Debian:
sudo apt-get update
sudo apt-get install php-imagick
sudo systemctl restart apache2
For CentOS/RHEL:
sudo yum install php-imagick
sudo systemctl restart httpd
For Windows:
  1. Download the appropriate DLL for your PHP version from PECL.

  2. Add the DLL to your PHP ext directory.

  3. Enable the extension in your php.ini:

    extension=imagick
  4. Restart your web server.

Verify installation:

php -m | grep imagick

Option 2: Use SVG Format Instead of PNG

SVG is a lightweight vector format that doesn’t require Imagick. To avoid the error, use the default SVG format:

This eliminates the dependency on Imagick.


Best Practices

  1. Use SVG Format: Unless you explicitly need PNG or JPG, SVG is more efficient for storing and displaying QR codes.

  2. Database Column Type: Use LONGTEXT for storing SVG content, as QR codes can be lengthy.

  3. Error Handling: Always validate data before generating QR codes to avoid runtime errors.

  • Programming
showkat ali Author

showkat ali

Greetings, I'm a passionate full-stack developer and entrepreneur. I specialize in PHP, Laravel, React.js, Node.js, JavaScript, and Python. I own interviewsolutionshub.com, where I share tech tutorials, tips, and interview questions. I'm a firm believer in hard work and consistency. Welcome to interviewsolutionshub.com, your source for tech insights and career guidance.

0 Comments

Post Comment

Recent Blogs

Recent posts form our Blog

How to Create a Custom Signup Form in Django 5.0

How to Create a Custom Signup Form in Django 5.0

Qadir Hassan
/
Programming

Read More
How to Start Your Journey as a Data Scientist|2025

How to Start Your Journey as a Data Scientist|2025

showkat ali
/
Programming

Read More
Complete Guide to Generating and Storing QR Codes in Laravel 11

Complete Guide to Generating and Storing QR Codes in Laravel 11

showkat ali
/
Programming

Read More
Node Version Manager (NVM) Install Guide: Windows, Linux, and macOS

Node Version Manager (NVM) Install Guide: Windows, Linux, and macOS

showkat ali
/
Technology

Read More
How to Handle Large Databases in Laravel: Best Practices for Managing Large Tables and Migrations

How to Handle Large Databases in Laravel: Best Practices for Managing Large Tables and Migrations

showkat ali
/
Programming

Read More
The Impact of AI on Film and TV Production

The Impact of AI on Film and TV Production

showkat ali
/
News

Read More