Kritim Yantra
Apr 05, 2025
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
Two popular Laravel-compatible packages for QR generation are:
composer require simplesoftwareio/simple-qrcode
composer require endroid/qr-code
use SimpleSoftwareIO\QrCode\Facades\QrCode;
public function generateQR()
{
$qrCode = QrCode::size(200)->generate('https://laravel.com');
return view('qr-code', compact('qrCode'));
}
<div>
{!! $qrCode !!} <!-- Renders as SVG by default -->
</div>
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()]);
}
<img src="{{ $qrCode }}" alt="QR Code">
QrCode::size(300)
->backgroundColor(255, 255, 0)
->color(0, 0, 255)
->generate('Hello World');
$logo = public_path('logo.png');
$qrCode = QrCode::create('https://laravel.com')
->setSize(300)
->setLogoPath($logo)
->setLogoSize(80);
// SVG
QrCode::format('svg')->generate('Data');
// PNG (default)
QrCode::format('png')->generate('Data');
// PDF (Endroid only)
$qrCode->setWriter(new PdfWriter());
return response()->streamDownload(
function () use ($qrCode) {
echo QrCode::size(200)->format('png')->generate('Download Me');
},
'qr-code.png',
['Content-Type' => 'image/png']
);
$qrCode->setWriter(new PdfWriter());
header('Content-Type: application/pdf');
echo $qrCode->writeString();
// Generate Google Authenticator QR
$google2faUrl = (new Google2FA)->getQRCodeUrl(
config('app.name'),
$user->email,
$user->google2fa_secret
);
$qrCode = QrCode::size(150)->generate($google2faUrl);
$paymentLink = "paypal.me/yourusername/10USD";
$qrCode = QrCode::size(200)->generate($paymentLink);
$ticketData = json_encode([
'event_id' => 1,
'user_id' => Auth::id(),
'hash' => Str::random(32),
]);
$qrCode = QrCode::size(250)->generate($ticketData);
✔ 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.
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.
✅ 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!
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 10, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google