Reader Stacks

How to Generate a QR Code in Laravel

A QR code package turns any string — a URL, a ticket ID, WiFi credentials — into a scannable SVG or PNG in a single method call.

Generating a QR code in Laravel is a matter of encoding a string (a URL, a ticket ID, WiFi credentials) into a scannable image — a dedicated package handles the actual QR encoding algorithm, which isn't something worth implementing from scratch.

Installing simple-qrcode

composer require simplesoftwareio/simple-qrcode

Generating a basic QR code

use SimpleSoftwareIO\QrCode\Facades\QrCode;

$qr = QrCode::size(300)->generate('https://example.com/tickets/abc123');
{!! QrCode::size(300)->generate('https://example.com/tickets/abc123') !!}

By default this generates an SVG — using {!! !!} (unescaped output) rather than {{ }} in the Blade template is necessary specifically because the raw SVG markup would otherwise be HTML-escaped and shown as literal text instead of rendered as an image.

Generating a PNG instead of SVG

QrCode::format('png')->size(300)->generate('https://example.com/tickets/abc123');

PNG output requires the Imagick PHP extension to be installed — SVG output (the default) has no such dependency, which is why SVG is often the simpler choice unless a PNG file is specifically needed (for embedding in a PDF via DomPDF, for instance, which doesn't reliably render inline SVG).

Saving a QR code directly to a file

QrCode::format('png')->size(300)->generate(
    'https://example.com/tickets/abc123',
    storage_path('app/public/qrcodes/ticket-abc123.png')
);

Adding a color and error correction level

QrCode::size(300)
    ->color(30, 60, 114)
    ->errorCorrection('H')
    ->generate($url);

A higher error correction level (H, the highest of the standard L/M/Q/H levels) lets the QR code remain scannable even if part of it is damaged, obscured, or has a logo overlaid on it — at the cost of a visually denser code, since higher error correction requires more redundant data encoded into the same physical size.

Adding a logo in the center

QrCode::size(300)
    ->merge('images/logo.png', 0.2, true)
    ->generate($url);

The 0.2 here caps the logo at 20% of the QR code's total size — going much larger risks obscuring enough of the encoded data that the code becomes unscannable even with high error correction enabled.

Common practical uses

Event ticket check-in, linking a printed flyer to a web page, sharing WiFi credentials (encoded in a specific WIFI: string format many phone cameras recognize automatically), and two-factor authentication setup (encoding a TOTP secret URI) are all common, genuinely useful applications of a generated QR code in a real application.