Skip to main content
The generate() method is the terminal call that renders a QR code from whatever payload and styling you have configured on the builder. Every fluent method (size(), color(), style(), and so on) returns a cloned Generator instance, leaving the original untouched, so you can safely reuse a partially-configured base and override individual settings per call.

generate()

public function generate(string $text, ?string $filename = null): QrCodeResult
Render the QR code for $text and, if you supply a $filename, write the output to that path on disk. The method always returns a QrCodeResult regardless of whether a file was written.

Parameters

text
string
required
The data payload to encode — a URL, plain string, or the pre-formatted output from any data-type method.
filename
string | null
Absolute or app-relative path where the QR code should be saved (e.g. storage_path('app/qrcodes/qr.svg')). The directory must already exist and be writable; the package will not create it for you. When omitted, the QR code is generated in memory only.

Return value

QrCodeResult
object
An immutable value object that implements Stringable, Htmlable, and Responsable.

Exceptions

CannotWriteFileException is thrown when you supply a $filename and either the parent directory does not exist or the process lacks write permission. Always ensure the directory exists before calling generate() with a filename.

Fluent builder entry points

You do not need to construct a Generator manually. The QrCode facade exposes every configuration method as a static entry point. Each call returns a cloned Generator so the facade’s default state is never mutated.
size
int
Set the width and height of the QR code in pixels. Returns a cloned Generator.
QrCode::size(300)->generate('https://example.com');
format
string
Set the output format before calling generate(). Accepted values: svg, png, webp, eps. Returns a cloned Generator.
QrCode::format('png')->generate('https://example.com');
margin
int
Set the quiet-zone (whitespace) margin around the QR code in pixels. Returns a cloned Generator.
QrCode::margin(2)->generate('https://example.com');
errorCorrection
string
Set the Reed–Solomon error correction level. Accepted values: L (7%), M (15%), Q (25%), H (30%). Returns a cloned Generator.
QrCode::errorCorrection('H')->generate('Critical data');
encoding
string
Set the character encoding used when encoding the payload. Defaults to UTF-8. Returns a cloned Generator.
QrCode::encoding('UTF-8')->generate('Unicode: こんにちは');
Because every fluent method returns a clone, you can safely store a base configuration and derive variations from it without side-effects:
$base = QrCode::size(300)->errorCorrection('H');

$red = $base->color(220, 38, 38)->generate('Red QR');
$blue = $base->color(37, 99, 235)->generate('Blue QR');
// $base is unchanged

Examples

use Linkxtr\QrCode\Facades\QrCode;

public function show(): \Illuminate\Http\Response
{
    return QrCode::size(300)
        ->format('svg')
        ->generate('https://example.com');
}