Laravel 8 QR Code Generate Example

Websolutionstuff | Jun-30-2021 | Categories : Laravel

In this post we will see Laravel 8 qr code generate example. we will generate QR Code using simple-qrcode package. so we will learn how to create QR Code in laravel 8 using simple package.

Here, we are using simple-qrcode package, This package is very simple to use and generate QR Code in laravel 8,you can also learn from official side of Simple QrCode.

so, let's start to implement laravel 8 QR Code generate example.

 

Step 1: Install Simple QRCode Package

In the first step we will install simple-qrcode package using cli. if you don't have install laravel then first install or setup laravel project. so below command and past in your terninal

composer require simplesoftwareio/simple-qrcode

 

Step 2: Setup Configuration

in app.php file add service provider and aliase.

config/app.php

'providers' => [

    SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class

],

'aliases' => [

    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class

],

 

Step 3: Create Controller QRController

In this we will create QRController in App\Http\Controllers path. and code for generate QR Code

php artisan:make controller QRController

 

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRController extends Controller

{
    public function index()
    {
    	return view('qrcode.index');
    }
    
    public function create()
    {
        QrCode::generate('https://websolutionstuff.com', '../public/QRCode.svg');
	    return redirect()->route('qrcode.index');
    }
}

 

Three formats are currently supported; png, eps, and svg. To change the format use the following code:

Note : imagick is required in order to generate a png image.

QrCode::format('png');  //Will return a png image
QrCode::format('eps');  //Will return a eps image
QrCode::format('svg');  //Will return a svg image

 

 

You can change the size of a QR Code by using the size method. Simply specify the size desired in pixels using the following syntax:

QrCode::size(100);

 

Step 4: Create Routes for QR Code Generate

Now step 4 in add routes for generate QR Code in laravel 8

use App\Http\Controllers\QRController;

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

In this step create balde file in resource/views/qrcode/index.blade.php path.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>Laravel 8 QR Code Generate 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 8 QR Code Generate Example - Websolutionstuff</h2>
					<button class="btn btn-success" type="submit">Generate</button> 
					<a href="{{asset('QrCode.svg')}}" class="btn btn-primary" download>Download</a><br>
					<img class="img-thumbnail" src="{{asset('QrCode.svg')}}" width="150" height="150" style="margin-top: 20px">
				</div>
			</div>
		</form>
	</body>
</html>

 

and also you can directly generate QR Code in blade file using below code.

{!! QrCode::generate('https:www.websolutionstuff.com'); !!}


 and finally you will get output like below screen shot.

Laravel 8 QR Code Generate Example

 

Recommended Post
Featured Post
How to Build a Blog CMS with React JS?
How to Build a Blog CMS with R...

Are you a blogger who expresses his thoughts with the help of words? If yes, then you might have once thought to create...

Read More

Nov-13-2023

How To Create Table Using Migration In Laravel 10
How To Create Table Using Migr...

In this article, we will see how to create a table using migration in laravel 10. Migrations are like version contr...

Read More

Apr-21-2023

Laravel 9 whereColumn Query Example
Laravel 9 whereColumn Query Ex...

In this article, we will see laravel 9 whereColumn() query example. The whereCoulmn method is used to verify whethe...

Read More

Oct-21-2022

How to Upgrade from Angular 16 to Angular 17
How to Upgrade from Angular 16...

Hey everyone! If you're a developer working with Angular, you know how exciting it is when a new version is released...

Read More

Mar-18-2024