A Generate QR code in Laravel is a type of barcode that may be learn simply by a digital system that stores information as a series of pixels in a square-shaped grid. On this fashionable technological period, QR code is very important. All modern technology provides QR code-generating process.
Today we’re going to learn, the right way to generate QR codes in laravel with examples. After ending this tutorial, you’ll learn how to generate and control the size, colour, background colour of a QR code. So, let’s start.
Steps to Generate QR Code in Laravel
- Step 1: Create Laravel Project
- Step 2: Add Database Details
- Step 3: Install QR Code Package
- Step 4: Register QR Code Service
- Step 5: Build Controller
- Step 6: Add Route
- Step 7: Generate QR Codes in Blade View
- Step 8: Run Laravel App
Step 1: Create Laravel Project
First, head over to the terminal and use the suggested command to install the new project. Don’t forget to get inside the project folder.
composer create-project --prefer-dist laravel/laravel laravel-demo
cd laravel-demo
Step 2: Add Database Details
The .env
config file helps you establish the database connection, make sure to add the database credentials in this file.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME=database_user_name DB_PASSWORD=database_password
In case you are using MAMP local server in macOs; ensure to append UNIX_SOCKET and DB_SOCKET under database credentials in .env
file.
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Step 3: Install QR Code Package
Get into the command prompt, type the given command, and start installing the simplesoftwareio/simple-qrcode package; it profoundly helps create various sorts of QR codes within the laravel app.
composer require simplesoftwareio/simple-qrcode
Step 4: Register QR Code Service
You have to register the QR code services in to 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, ]
Also Read : The right way to Generate an OTP in Laravel
Step 5: Build Controller
In Laravel, all of the business logic goes into the controller file, and we’d like a controller to create one by using the given command.
php artisan make:controller QrCodeController
Next, head over to resources/views/QrCodeController.blade.php
and add the provided code.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class QrCodeController extends Controller { public function index() { return view('qrcode'); } }
Step 6: Add Route
In the controller we defined the function to load the view file, code in the controller is run by routes, so go ahead and define the new route in routes/web.php
file.
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\QrCodeController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- */ Route::get('/generate-qrcode', [QrCodeController::class, 'index']);
Step 7: Generate QR Codes in Blade View
We’ll show you use the view file and generate easy and colored QR Codes in laravel.
Now, you are able to set up a blade view file, therefore create the blade view file within the views folder, after that add the provided code within the resources/views/qrcode.blade.php
file.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel Generate QR Code Examples</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('RemoteStack') !!} </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('RemoteStack') !!} </div> </div> </div> </body> </html>
Step 8: Start Application
Finally, use the PHP artisan command to start out the Laravel server, also use the given url to view the app.
php artisan serve
Advantage of QR Code
Unless you’ve been hiding under a rock for the last decade, chances are you’ve come across QR codes in some form or another, at work as well as in your leisure time. These barcodes are a fantastic way to market your brand, and if you’re a B2B business, you can take advantage of them to promote your company. But what are the main advantages of QR codes and how can they help your B2B website perform better? We’re here to lay it out for you.
- Quick and error-free
- More informative
- Engaging with rich content
- Actionable
- Trackable
- Easy to save
Leave Your Comment