How To Generate QRcode In Laravel

Websolutionstuff | Jun-01-2020 | Categories : Laravel PHP CSS HTML Bootstrap

In this example, I will give information about how to generate QR code in laravel. As per the current trend, many websites and applications provide features like login with a QR code, scanning QR codes, getting more information about products and websites, etc.

So, let's see how to generate QR code in laravel 6/7/8, laravel 7/8 QR code generate example, QR code generator in laravel 6/7/8, endroid QR code generator, how to dynamic QR code generator in laravel 6/7/8, laravel QR code generator with logo, QR code generator laravel 7.

So, I will implement a QR code in my project to give you a demo, there are many packages available to generate QR codes. So, in this example, I will use Endroid QR Code Generator. Also, you can generate a dynamic QR code generator in laravel 6/7.

Let's start QR code example In laravel 6/7 or laravel QR code generate an example with the code and implement the below code in your application.

 Step 1: Install Laravel

Type the following command in the terminal to create a new project in your system.

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

 

 

Step 2: Install Endroid QRcode Package In Your Application

After installation of the project, you need to install Endroid QRcode Package

composer require endroid/qr-code

 

Step 3: Create Controller

Now, create a controller on this path 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(300);
		$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 codes and viewing files.

<?php

use Illuminate\Support\Facades\Route;

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

 

Step 5 :  Create Blade File

And after that, 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>Qrcode - 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>Click on button to generate Qrcode - 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 QRcode as per your requirements like changes in background colors, changes in foreground color, QRcode size, QRcode margin, etc...

 

 

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

http://localhost:8000/qr_code/index

 

And finally, you will get output like the below screen print.

qrcode

 


You might also like :

Recommended Post
Featured Post
How to Upload Multiple Image in Laravel 8
How to Upload Multiple Image i...

In this example we will see how to upload multiple image in laravel 8. here, we wil see tutorial of multiple image uploa...

Read More

Sep-17-2021

Autocomplete Search from Database in Laravel 11
Autocomplete Search from Datab...

Hello, laravel web developers! In this article, we'll see how to autocomplete a search from a database in larav...

Read More

Jun-24-2024

How To Get Current URL In React JS
How To Get Current URL In Reac...

As a React JS developer, I have come to appreciate the power and popularity of this remarkable JavaScript library. Its c...

Read More

Aug-09-2023

How To Generate PDF File In Laravel 10
How To Generate PDF File In La...

In this article, we will see how to generate a pdf file in laravel 10. Here, we will learn about laravel 10 ge...

Read More

Mar-10-2023