Generating QR Codes in Laravel 12: A Complete Guide

Author

Kritim Yantra

Apr 05, 2025

Generating QR Codes in Laravel 12: A Complete Guide

QR codes are widely used for quick data sharing, payment systems, authentication, and marketing campaigns. Laravel makes it easy to generate QR codes with just a few lines of code.

In this blog, we’ll cover:
Best Laravel QR code packages
Generating QR codes in controllers & blades
Customizing QR codes (size, color, logos)
Downloading & displaying QR codes
Real-world use cases


1. Best QR Code Packages for Laravel

Two popular Laravel-compatible packages for QR generation are:

A. Simple-QRcode (Bacon/BaconQrCode)

  • Lightweight, supports basic QR generation.
  • Installation:
    composer require simplesoftwareio/simple-qrcode
    

B. Laravel QR Code (Endroid/QrCode)

  • More advanced (custom styling, SVG support).
  • Installation:
    composer require endroid/qr-code
    

2. Generating a Basic QR Code

Using Simple-QRcode

In a Controller

use SimpleSoftwareIO\QrCode\Facades\QrCode;

public function generateQR()
{
    $qrCode = QrCode::size(200)->generate('https://laravel.com');
    return view('qr-code', compact('qrCode'));
}

In a Blade View

<div>
    {!! $qrCode !!} <!-- Renders as SVG by default -->
</div>

Using Endroid/QrCode

use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;

public function generateQR()
{
    $qrCode = QrCode::create('https://laravel.com')
        ->setSize(200);
    
    $writer = new PngWriter();
    $result = $writer->write($qrCode);
    
    // Save to file (optional)
    $result->saveToFile(public_path('qr-code.png'));
    
    return view('qr-code', ['qrCode' => $result->getDataUri()]);
}

Displaying in Blade

<img src="{{ $qrCode }}" alt="QR Code">

3. Customizing QR Codes

A. Changing Size & Color (Simple-QRcode)

QrCode::size(300)
    ->backgroundColor(255, 255, 0)
    ->color(0, 0, 255)
    ->generate('Hello World');

B. Adding a Logo (Endroid/QrCode)

$logo = public_path('logo.png');

$qrCode = QrCode::create('https://laravel.com')
    ->setSize(300)
    ->setLogoPath($logo)
    ->setLogoSize(80);

C. Formatting (SVG, PNG, PDF)

// SVG
QrCode::format('svg')->generate('Data');

// PNG (default)
QrCode::format('png')->generate('Data');

// PDF (Endroid only)
$qrCode->setWriter(new PdfWriter());

4. Downloading QR Codes

As PNG Download

return response()->streamDownload(
    function () use ($qrCode) {
        echo QrCode::size(200)->format('png')->generate('Download Me');
    },
    'qr-code.png',
    ['Content-Type' => 'image/png']
);

As PDF Download (Endroid)

$qrCode->setWriter(new PdfWriter());
header('Content-Type: application/pdf');
echo $qrCode->writeString();

5. Real-World Use Cases

A. User Authentication (2FA QR Codes)

// Generate Google Authenticator QR
$google2faUrl = (new Google2FA)->getQRCodeUrl(
    config('app.name'),
    $user->email,
    $user->google2fa_secret
);

$qrCode = QrCode::size(150)->generate($google2faUrl);

B. Dynamic Payment QR (Stripe/PayPal)

$paymentLink = "paypal.me/yourusername/10USD";
$qrCode = QrCode::size(200)->generate($paymentLink);

C. Event Tickets

$ticketData = json_encode([
    'event_id' => 1,
    'user_id' => Auth::id(),
    'hash' => Str::random(32),
]);

$qrCode = QrCode::size(250)->generate($ticketData);

6. Best Practices

Cache QR codes if generating frequently (e.g., for tickets).
Sanitize input to prevent malicious QR codes.
Use HTTPS URLs for security.
Test readability with different QR scanners.


Conclusion

Laravel makes QR code generation simple and flexible with packages like Simple-QRcode and Endroid/QrCode. Whether you need basic barcodes or custom-styled QR codes with logos, Laravel has you covered.

Key Takeaways

✅ Use Simple-QRcode for basic needs, Endroid for advanced styling.
✅ Customize size, color, and format easily.
✅ Add logos for branding.
✅ Generate QR codes for 2FA, payments, tickets, and more.

🚀 Ready to integrate QR codes into your Laravel app? Try it today!

Need help? Drop a comment below!

Tags

Laravel Php

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Continue with Google

Related Posts