Hi everyone! If you’re building a Laravel 12 application and want to add QR codes—for links, emails, phone numbers, or even custom designs—you’re in the right place. In this simple guide, I’ll show you how to generate QR codes using the simplesoftwareio/simple-qrcode package.
We’ll cover everything from basic QR codes to advanced ones with colors, images, and more.
Before we begin, ensure you have:
If you don’t have a project yet, don’t worry—I’ll guide you through the setup.
If you already have a Laravel 12 project, skip this step. Otherwise, create a new project by running:
laravel new example-app
This command sets up a fresh Laravel 12 application named example-app
.
To generate QR codes, we’ll use the simplesoftwareio/simple-qrcode
package. Open your terminal and run:
composer require simplesoftwareio/simple-qrcode
This package makes it easy to create and customize QR codes in Laravel.
Let’s create a route to display the QR code. Open the routes/web.php
file and add:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QRCodeController;
Route::get('qr-code', [QRCodeController::class, 'index'])->name('qr.code');
This route points to an index
method in a QRCodeController
that we’ll create next.
Run the following command to create a controller:
php artisan make:controller QRCodeController
Now, let’s explore different ways to generate QR codes by updating the app/Http/Controllers/QRCodeController.php
file. Below are eight examples, each showing a unique way to create a QR code.
This creates a QR code for a URL, like a website link.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
public function index()
{
return QrCode::size(300)->generate('https://www.techsolutionstuff.com');
}
}
Output: A 300x300 pixel QR code that, when scanned, directs to https://www.techsolutionstuff.com
.
This generates a QR code and saves it as a PNG file in the public/qrcode
folder.
Note: Create a qrcode
folder in the public
directory before running this code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
public function index()
{
$path = public_path('qrcode/'.time().'.png');
QrCode::size(300)->generate('A simple QR code example', $path);
return response()->download($path);
}
}
Output: The QR code is saved as a PNG file (e.g., public/qrcode/1234567890.png
) and downloaded.
This adds a custom background color to the QR code (e.g., orange).
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
public function index()
{
return QrCode::size(300)
->backgroundColor(255, 55, 0)
->generate('A simple QR code example');
}
}
Output: A 300x300 QR code with an orange background.
This embeds an image (e.g., a logo) in the center of the QR code.
Note: Place an image (e.g., logo.png
) in the public/images
folder and update the path in the code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
public function index()
{
$image = QrCode::format('png')
->merge(public_path('images/logo.png'), 0.5, true)
->size(500)
->errorCorrection('H')
->generate('A simple QR code example');
return response($image)->header('Content-type', 'image/png');
}
}
Output: A 500x500 QR code with your logo in the center.
This creates a QR code that opens an email client with predefined details.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
public function index()
{
return QrCode::size(500)
->email('[email protected]', 'Welcome!', 'This is a test email.');
}
}
Output: A QR code that, when scanned, opens an email client with the specified recipient, subject, and body.
This creates a QR code that dials a phone number when scanned.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
public function index()
{
return QrCode::size(300)->phoneNumber('123-456-7890');
}
}
Output: A QR code that initiates a call to 123-456-7890
.
This creates a QR code that opens a messaging app with a predefined number and message.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
public function index()
{
return QrCode::size(300)->SMS('123-456-7890', 'Hello, this is a test message!');
}
}
Output: A QR code that opens a messaging app with the number and message pre-filled.
You can display a QR code directly in a Blade view. Create a file named qrCode.blade.php
in resources/views
and add:
<!DOCTYPE html>
<html>
<head>
<title>Laravel 12 QR Code Example</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container mt-5 text-center">
<h3>Laravel 12 QR Code Example</h3>
<div class="mt-4">
{!! QrCode::size(200)->generate('Welcome to my website!') !!}
<p>Scan the QR code above.</p>
</div>
</div>
</body>
</html>
Update the QRCodeController.php
to return this view:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QRCodeController extends Controller
{
public function index()
{
return view('qrCode');
}
}
Output: A webpage displaying a 200x200 QR code that, when scanned, shows Welcome to my website!
.
To test your QR codes, start the Laravel server:
php artisan serve
Visit http://localhost:8000/qr-code
in your browser to see the QR code. For each example, update the QRCodeController
with the desired code and refresh the page. Use a QR code scanner (e.g., on your phone) to test the output.
Generating QR codes in Laravel 12 is a breeze with the simplesoftwareio/simple-qrcode
package! Whether you’re creating QR codes for links, emails, phone numbers, or custom designs, this tutorial has shown you how to do it all.
I hope this guide was easy to follow and inspires you to add QR codes to your Laravel projects. Try experimenting with different sizes, colors, or embedded images to make your QR codes stand out.
Q1: What types of data can I encode in a QR code?
A: You can encode URLs, text, email addresses, phone numbers, SMS messages, and more. The simple-qrcode
package supports various formats via methods like email()
, phoneNumber()
, and SMS()
.
Q2: Why is my QR code not displaying in the browser?
A: Ensure you’re returning the QR code with the correct content type (e.g., image/png
) for direct image outputs. For Blade templates, check that the QrCode
facade is properly used with {!! !!}
.
Q3: Can I change the QR code size or color?
A: Yes! Use the size()
method to adjust dimensions (e.g., size(500)
) and backgroundColor()
or color()
to change colors (e.g., backgroundColor(255, 55, 0)
).
Q4: Why do I get an error when saving the QR code?
A: Make sure the public/qrcode
folder exists and has write permissions. Create it manually if needed (mkdir public/qrcode
).
Q5: How do I test if my QR code works?
A: Use a QR code scanner app on your smartphone or a browser extension. Scan the QR code to verify it directs to the correct URL, email, or other data.
You might also like: