QrCode facade uses Laravel’s Macroable trait, which lets you attach custom methods at runtime — no subclassing or package forks required. Macros are the cleanest way to encapsulate application-specific QR payloads and share them everywhere in your codebase.
How Macros Work
When you call an unrecognized method on theQrCode facade (such as QrCode::Spotify(...)), the Generator checks whether a macro with that name is registered. If one exists, it calls your closure and handles the return value automatically:
- Return a
string— the Generator encodes it into a QR code using the current config defaults. - Return a
QrCodeResult— the Generator passes it through as-is, so all styling you applied inside the macro is preserved.
string or the result of $this->generate().
Inside a macro closure,
$this refers to the Generator instance. You can
call any fluent builder method — size(), margin(), format(), color(),
and so on — directly on $this.Registering Macros
Register macros in theboot() method of your App\Providers\AppServiceProvider. This ensures they are available for every request after the service container finishes booting.
Two Registration Approaches
Approach 1 — Return a String Payload
Return a plain string from your macro. The Generator automatically encodes it into a QR code using whatever config defaults are active. Use this approach when you only need to format a payload string and do not care about styling inside the macro.spotify:track:4uLU6hMCjMI75M1A2tKUQC and generates a QR code with your global defaults applied.
Approach 2 — Return a Pre-Styled QrCodeResult
Call $this->generate(...) inside the macro and return the result. This lets you bake size, margin, format, color, and any other styling into the macro itself.
Use this approach when the macro should always produce a QR code with a specific look — for example, a company asset tag that must always be 300 px with a tight margin.
Full AppServiceProvider Example
Using Macros in Blade and Controllers
Once registered, macros behave exactly like the built-in data-type methods. Call them directly on the facade from any Blade template or PHP class.Chaining Before a Macro Call
You can still chain builder methods before calling a macro. The chained options are applied to the clonedGenerator instance that the macro’s $this receives.