Laravel 11 allows you to dynamically generate QR codes using various libraries. One popular choice is the simple-qrcode package. Here’s how you can generate QR codes in Laravel 11, along with storing QR code images on the server.
Table Of Content
1 Prerequisites
1.) PHP version of 8.2
2.) Composer
2 Introduction
A QR code, or Quick Response code, is a 2D barcode that stores information. This guide will show how to dynamically generate QR codes using Laravel 11 and store QR code images on the server.
3 Install Laravel Project
First, ensure Composer is installed, then run the following to create a new Laravel project:
To generate QR codes in Laravel 11, install the simple-qrcode package:
composer require simplesoftwareio/simple-qrcode
5 Generate QR Code Examples
Here are some examples to generate Various types of QR code images using Laravel.
1.URL
Specify the website URL including the protocol (HTTP or HTTPS) to recognize the QR code as a URL.
$qrContent = 'https://getsamplecode.com/';
2.TEXT:
Specify the Text to Generate QR Code.
$qrContent = 'QR Code Generated by GetSampleCode';
3.Phone Number
Specify the Phone Number including Country Code to Generate QR Code.
$qrContent = 'tel:+16471234567';
4.SMS
Specify the Phone Number including Country Code and pre filled message to Generate QR Code.
$qrContent = 'sms:+16471234567:Samplemessage';
5.EMAIL
Specify the Email Address to Generate QR Code.
$qrContent = ''mailto:getsamplecode@gmail.com';';
6 Create New Controller - QrcodeController
Create a QrCodeController to handle the logic for generating QR codes:
php artisan make:controller QrCodeController
In the controller, you can define methods for displaying and generating QR codes dynamically based on user input. This allows for flexibility in generating QR codes for URLs, texts, phone numbers, and more.
Next, create a qrcode.blade.php view file to display the form for QR code generation. Users will select the type of QR code they want to generate (e.g., text, URL, email) and input the necessary data.
Define routes in web.php for accessing the QR code generation:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QRCodeController;
Route::get('/', function () {
return view('welcome');
});
Route::get('index', [ QRCodeController::class, 'index' ]);
Route::post('/generate-qrcode', [QRCodeController::class, 'generate'])->name('generate');
9 Storing QR Codes on the Server
After generating the QR code, you can store the QR code image on the server using Laravel’s storage functionality:
Storage::disk('local')->put($saveName, $qrCode);
10 Folder Structure
The folder structure will look like this:
11 Run Laravel Server to Test the App
Use the following artisan command to Test the App.
php artisan serve
Visit the URL http://127.0.0.1:8000
12 Conclusion
With these steps, you can easily generate QR codes in Laravel 11 and store QR code images on the server dynamically. This guide offers flexibility for generating QR codes with various types of content, from URLs to emails.