How to Generate QR Codes in Laravel 11 Example

Hello developers! In this guide, we'll see how to generate QR codes in laravel 11. Here, we'll use endroid QR code package in laravel 11. This library helps you generate QR codes in a jiffy. the trend of incorporating QR code functionality into websites and applications is gaining popularity.

Features like login with a QR code, scanning QR codes to access information about products and websites, and more are becoming increasingly common. QR codes offer convenience, efficiency, and enhanced user experiences.

We'll generate dynamic QR codes in laravel 11. we'll use Endroid QR Code package.

Laravel 11 Generate QR Code

Laravel 11 Generate QR Code

 

Step 1: Install Laravel 11 Application

In this step, we'll install the laravel 11 application using the following command.

composer create-project --prefer-dist laravel/laravel Laravel_11_QRCode

 

 

Step 2: Install Endroid QR Code Package

Now, we will install the Endroid QR Code package using the following command.

composer require endroid/qr-code

 

Step 3: Create Controller

Then, we'll create a QRController.php file and in this file, we'll define the function and generate the QR code.

app\Http\Controllers\QRController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;

class QRController extends Controller
{
    public function index()
    {
    	return view('qrcode.index');
    }
    
    public function create()
    {
		$qrCode = new QrCode('techsolutionstuff.com');
		$qrCode->setSize(300);
		$qrCode->setMargin(20); 
		$qrCode->setEncoding('UTF-8');
		$qrCode->setWriterByName('png');
		$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
		$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
		$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
		$qrCode->setLogoSize(200, 200);
		$qrCode->setValidateResult(false);		
		$qrCode->setRoundBlockSize(true);
		$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);
		header('Content-Type: '.$qrCode->getContentType());
		$qrCode->writeFile(public_path('/qrcode.png'));

		return redirect()->route('qrcode.index');
    }
}

 

 

Step 4: Add Route

In this step, we'll define the routes in the web.php file.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QRController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('qr_code/index',[QRController::class,'index'])->name('qrcode.index');
Route::get('qr_code/create',[QRController::class,'create'])->name('qrcode.create');

 

Step 5: Create Blade File

Now, we will create a blade file for viewing.

 index.blade.php

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>How to Generate QR Codes in Laravel 11 Example - Techsolutionstuff</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	</head>
	<body>
		<form class="text-center" action="{{route('qrcode.create')}}" method="get" accept-charset="utf-8">
			<div class="row mt-5">
				<div class="col-md-12">
					<h2>How to Generate QR Codes in Laravel 11 Example - Techsolutionstuff</h2>
					<button class="btn btn-success" type="submit">Generate</button> 
					<a href="{{asset('qrcode.png')}}" class="btn btn-primary" download>Download</a><br>
					<img class="img-thumbnail" src="{{asset('qrcode.png')}}" width="150" height="150" style="margin-top: 20px">
				</div>
			</div>
		</form>
	</body>
</html>

You can customize this QR code as per your requirements like background colors, foreground color, QR code size, QR code margin, etc...

 

Step 6: Run the Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS