How to Generate QR Code in Laravel 11?
In this post, I will show you how to generate a QR code in a Laravel 11 application. We will generate and save the QR code for a link.
We will use the simplesoftwareio/simple-qrcode composer package to generate QR codes in Laravel 11. simplesoftwareio/simple-qrcode provides methods to generate QR codes, save QR codes, generate QR codes for link, generate QR codes for phone numbers, generate QR codes for emails, and generate QR codes with downloads. So, we will see one-by-one example code.
Step for Laravel 11 Generate QR Code Example
- Step 1: Install Laravel 11
- Step 2: Install simplesoftwareio/simple-qrcode
- Step 3: Create Route
- 1: Laravel Generate QR Code for Link Example
- 2: Laravel Generate QR Code and Save Example
- 3: Laravel Generate QR Code with Color Example
- 4: Laravel Generate QR Code with Image Example
- 5: Laravel Generate Email QR Code Example
- 6: Laravel Generate Phone QR Code Example
- 7: Laravel Generate SMS QR Code Example
- 8: Laravel Generate QR Code in Blade File Example
Let's see the below steps, and you can generate QR code in your Laravel 11 projects as well.
Install Laravel 11
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Install simplesoftwareio/simple-qrcode
In the first step, we will install the simplesoftwareio/simple-qrcode Package that provides the capability to generate QR codes in a Laravel application. So, first open your terminal and run the below command:
composer require simplesoftwareio/simple-qrcode
Create Route
In this step, we will add routes and a controller file. So first, add the below route in your routes.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QRCodeController;
Route::get('qr-code', [QRCodeController::class, 'index']);
1: Laravel Generate QR Code for Link Example
We will use the `size()` and `generate()` methods to create a QR code for the link. You can see the controller file code:
app/Http/Controllers/QRCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return QrCode::size(300)->generate('https://www.itsolutionstuff.com');
}
}
Output:
2: Laravel Generate QR Code and Save Example
We will use the `size()` and `generate()` methods to create a QR code and save it. You need to create a "qrcode" folder in the public folder. You can see the controller file code:
app/Http/Controllers/QRCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$path = public_path('qrcode/'.time().'.png');
return QrCode::size(300)
->generate('A simple example of QR code', $path);
}
}
3: Laravel Generate QR Code with Color Example
We will use the `size()`, `generate()` and `backgroundColor()` methods to create a QR code with background color. You can see the controller file code:
app/Http/Controllers/QRCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return QrCode::size(300)
->backgroundColor(255,55,0)
->generate('A simple example of QR code');
}
}
Output:
4: Laravel Generate QR Code with Image Example
We will use the `size()`, `generate()`, and `merge()` methods to create a QR code with an image. You need to provide the path of the image. You can see the controller file code:
app/Http/Controllers/QRCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$image = QrCode::format('png')
->merge(public_path('images/YOUR_IMAGE_NAME.png'), 0.5, true)
->size(500)
->errorCorrection('H')
->generate('A simple example of QR code!');
return response($image)->header('Content-type','image/png');
}
}
Output:
5: Laravel Generate Email QR Code Example
We will use the `size()`, and `email()` methods to create a QR code with for email. You can see the controller file code:
app/Http/Controllers/QRCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return QrCode::size(500)
->email('hardik@itsolutionstuff.com', 'Welcome to ItSolutionStuff.com!', 'This is !.');
}
}
6: Laravel Generate Phone QR Code Example
We will use the `size()`, and `phoneNumber()` methods to create a QR code with for phone number. You can see the controller file code:
app/Http/Controllers/QRCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return QrCode::phoneNumber('111-222-6666');
}
}
7: Laravel Generate SMS QR Code Example
We will use the `size()`, and `SMS()` methods to create a QR code with for sending sms. You can see the controller file code:
app/Http/Controllers/QRCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class QRCodeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
QrCode::SMS('111-222-6666', 'Body of the message');
}
}
8: Laravel Generate QR Code in Blade File Example
Here, You can generate OR code in blade file:
Code:
<div class="visible-print text-center">
{!! QrCode::size(100)->generate('Demo'); !!}
<p>Scan me to return to the original page.</p>
</div>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Model Events Example Tutorial
- Laravel 11 Multi Auth: Create Multiple Authentication in Laravel 11
- Laravel 11 Resource Route and Controller Example
- Laravel 11 Scout Full Text Search Tutorial
- Laravel 11 Autocomplete Search from Database Example
- Laravel 11 Flash Message Example Tutorial
- Laravel 11 REST API Authentication using Sanctum Tutorial
- Laravel 11 Authentication - Install Laravel 11 Breeze Tutorial
- Laravel 11 Bootstrap Auth Scaffolding Tutorial
- How to Send Email using Gmail in Laravel 11?
- Laravel 11 Import Export Excel and CSV File Tutorial
- Laravel 11 Multiple Image Upload Tutorial Example
- Laravel 11 CRUD Application Example Tutorial
- How to Register Schedule Command in Laravel 11?