Laravel 9 QR Code Generator Example

Websolutionstuff | Mar-25-2022 | Categories : Laravel PHP

In this article, we will see laravel 9 QR code generator example. As per the current trend, many websites and applications provide features like login with QR code, scan QR code and get more information about products and websites, etc. we will implement a QR code example. there are many packages available to generate QR code. we will use Endroid QR Code Generator. Also, you can generate a dynamic QR code generator in laravel 9.

So, let's see how to generate QR code in laravel 9, laravel 9 generate QR code, generate QR code laravel 9, how to generate dynamic QR code in laravel 9.

Step 1 : Install Laravel 9

Step 2 : Install Endroid QR Code Package

Step 3 : Create Controller

Step 4 : Add Route

Step 5 :  Create Blade File

 

 Step 1 : Install Laravel 9

In this step, we will create laravel 9 application using the below command.

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

 

 

Step 2 : Install Endroid QR Code Package

After installation of the project, you need to install Endroid QR Code Package using the below command.

composer require endroid/qr-code

 

Step 3 : Create Controller

Now create a controller app\Http\Controllers\QRController.php and add the below command.

<?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('websolutionstuff.com');
		$qrCode->setSize(500);
		$qrCode->setMargin(10); 
		$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(150, 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

We need to add a route for generating QR code and viewing files.

<?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 need to create a blade file for viewing / generating, and downloading QR codes. So, add the below code in your index.blade.php file

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>Laravel 9 QR Code Generator Example - Websolutionstuff</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>Laravel 9 QR Code Generator Example - Websolutionstuff</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...

 

 

To check this example run the below code in your browser.

http://localhost:8000/qr_code/index

Output :

laravel_9_qr_code_generator_example_output

 


You might also like :

Recommended Post
Featured Post
Laravel Datatables Example
Laravel Datatables Example

In this example, I will show you how to implement/install data tables in laravel. Datatables provides users with many fu...

Read More

May-16-2020

Get User Location using IP Address in Laravel 11
Get User Location using IP Add...

Hello developers! In this article, we'll see how to get user location using an IP address in laravel 11. Here,...

Read More

May-01-2024

How To Add Bootstrap Modal In React JS
How To Add Bootstrap Modal In...

In this article, we will see how to add a bootstrap modal in react js. In this example, we will use the bootstrap m...

Read More

Sep-09-2022

Pagination Example In Laravel
Pagination Example In Laravel

In this article, we will see a pagination example in laravel, as we all know pagination is a very common feature in all...

Read More

Sep-24-2020