TechvBlogs
Published in

TechvBlogs

How to Generate QR Code in Laravel 8

Nov 17, 2021, Originally published at techvblogs.com ・3 min read

QR codes are sometimes needed for product identity, inventory and others. Then, how to generate a QRcode? it’s easy, we can use Simple QRcode to create QRcode in laravel framework.

Nowadays, we all know how much use the QR code. QR code is simply an encrypted image of some content that is not readable. It needs to use some of the QR code readers.

In this article, we will both learn how to create or generate a QRcode so that when we scan the code it can be directed to SMS, email, website or just to find out what data is behind the QR code.

In this article, we will start from scratch by starting with creating a new laravel project.

This guide will take you through all the necessary steps, which will tell you how to generate various QR codes in the Laravel 8 application using the simple QR code package.

A simple QR code generator gives you the freedom to generate different types of QR Codes in the Laravel 8 app. It gives a simple QrCode wrapper, which is easy to integrate into laravel.

How to Generate QR Code in Laravel 8

  • Create Laravel Project
  • Add Database Details
  • Install QR Code Package
  • Register QR Code Service
  • Create Controller
  • Add Route
  • Generate QR Codes in Blade View
  • Run Laravel App

Create Laravel Project

First, open Terminal and run the following command to create a fresh Laravel project:

composer create-project --prefer-dist laravel/laravel qr-code-example

or, if you have installed the Laravel Installer as a global composer dependency:

laravel new qr-code-example

Add Database Details

After, Installation Go to the project root directory, open the .env file, and set database detail as follow:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=<DATABASE NAME>
DB_USERNAME=<DATABASE USERNAME>
DB_PASSWORD=<DATABASE PASSWORD>

Install QR Code Package

Get into the command prompt, type the given command, and begin installing the simplesoftwareio/simple-qrcode package; it profoundly helps create various kinds of QR codes in the laravel app.

composer require simplesoftwareio/simple-qrcode

Register QR Code Service

You have to register the QR code services into the config/app.php file, so open the file and update the providers and alias array with the given below services.

<?php    return [    'providers' => [
....
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
],

'aliases' => [
....
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
]

Create Controller

In laravel, all the business logic goes into the controller file, and we need a controller to create one by using the given command.

php artisan make:controller QrCodeController

Next, Open QrCodeController.php and add the following code into the file:

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class QrCodeController extends Controller
{
public function index()
{
return view('qrcode');
}
}

Add Route

Now, open the web.php file and add the following routes into it, which is located inside the routes directory:

<?phpuse Illuminate\Support\Facades\Route;
use App\Http\Controllers\QrCodeController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('/qrcode', [QrCodeController::class, 'index']);

Generate QR Codes in Blade View

We will show you how to use the view file and generate simple and colored QR Codes in laravel.

Now, you are ready to set up a blade view file, hence creating the blade view file within the views folder, after that add the provided code in the resources/views/qrcode.blade.php file.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>How to Generate QR Code in Laravel 8</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body> <div class="container mt-4"> <div class="card">
<div class="card-header">
<h2>Simple QR Code</h2>
</div>
<div class="card-body">
{!! QrCode::size(300)->generate('https://techvblogs.com/blog/generate-qr-code-laravel-8') !!}
</div>
</div>
<div class="card">
<div class="card-header">
<h2>Color QR Code</h2>
</div>
<div class="card-body">
{!! QrCode::size(300)->backgroundColor(255,90,0)->generate('https://techvblogs.com/blog/generate-qr-code-laravel-8') !!}
</div>
</div>
</div>
</body>
</html>

Run Laravel App

Eventually, use the PHP artisan command to start the laravel server, also use the given URL to view the app.

php artisan serve

Thank you for reading this blog.

--

--

--

We are sharing Blogs that are related to 🌍 Web and 📱 Mobile Applications issues and examples of Development. We are share blogs related to Laravel, PHP, CodeIgniter, HTML, CSS, Bootstrap, Javascript, jQuery, MySQL, MongoDB, Node.js, Vue.js, Nuxt.js.

Recommended from Medium

Let’s start!

Finding Python

Consistent Hashing in Simple Terms

Understanding the modern C++ build environment

Fast Pages free blogging platform in GitHub pages free hosting with support for Jupyter notebooks…

Forensic on AWS. Basic services and actions.

Luart Update/AMA Recap: TGE, v1, and more

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Smit Pipaliya

Smit Pipaliya

I am Senior Developer at ServerAvatar Technology.

More from Medium

How to Enable and Disable Debug Mode in Laravel

Laravel — Installation

Installation laravel

Laravel 9

How to change password in laravel 9