Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

auxit's avatar
Level 4

Can't access $request variable from Fortify 2FA documentation

Hello. There's a section in the Fortify docs (https://laravel.com/docs/12.x/fortify#enabling-two-factor-authentication) that says the following:

"Next, you should display the two factor authentication QR code for the user to scan into their authenticator application. If you are using Blade to render your application's frontend, you may retrieve the QR code SVG using the twoFactorQrCodeSvg method available on the user instance:"

$request->user()->twoFactorQrCodeSvg();

When I try {{$request->user()->twoFactorQrCodeSvg()}} in my blade template I get an error that says "Undefined variable $request." I think I'm misunderstanding what they're trying to tell me to do exactly. I can use the following instead {!! request()->user()->twoFactorQrCodeSvg() !!}, but I don't understand why.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue you're encountering is due to the fact that the $request variable is not automatically available in Blade templates. In Blade, you typically don't have direct access to the $request variable unless you explicitly pass it to the view.

However, Laravel provides a global request() helper function that you can use to access the current request instance. This is why using {!! request()->user()->twoFactorQrCodeSvg() !!} works for you.

Here's a breakdown of the solution:

  1. Using the request() Helper: The request() helper function is globally available in Laravel and returns the current request instance. This allows you to access the authenticated user and call the twoFactorQrCodeSvg() method on it.

  2. Blade Syntax: When you want to render HTML content (like an SVG) in a Blade template, you should use {!! !!} instead of {{ }}. The {{ }} syntax escapes HTML by default to prevent XSS attacks, while {!! !!} allows you to render raw HTML.

Here's how you can correctly use it in your Blade template:

{!! request()->user()->twoFactorQrCodeSvg() !!}

This will render the QR code SVG for the authenticated user without causing an "undefined variable" error.

Please or to participate in this conversation.