In this article we will learn to generate the qr code in Laravel. we will use a simple example to generate the qr code. As we know qr code is stand for Quick Response
and we can scan and get the information from it. we can use qr code in multiple way like to share the coupon, GPS, payment etc.
In this tutorial we will user simple-qrcode
package of Laravel 8 application to generate the qr code. This package is capable of generating phone number, email, geo, colored qr code, image etc.
Before start i assume you know how to install and setup Laravel if not then you can read this guide How to install laravel 8 ?.
Let’s begin the tutorial Qr Code Generate in Laravel Online with simple steps
Step 1: Install simple-qrcode Package
After installation of Laravel 8, our first step is to install the simple-qrcode package, so open the terminal in project and type below command
composer require simplesoftwareio/simple-qrcode
This will add simple-qrcode package to our project.
Step 2: Add package in service provider
Now, we have installed the package but not registered in our service provider. open file config/app.php
'providers' => [
....
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class
],
'aliases' => [
....
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
]
Here, we added QrCodeServiceProvider and aliases to access the QrCode package in our application with simple name.
Step 4: Create Route
we have installed and defined the package in our app.php. now, it’s time to execute and run it. so, create the route in web.php
<?php
use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\QRController;
Route::get('/view-qr-code',[QRController::class, 'view']);
Step 5: Create Controller
Now, generate the controller for route
php artisan make:controller QrCodeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QRController extends Controller
{
public function view()
{
$qrCodeSimple = \QrCode::size(300)->generate('ReaderStacks');
return view('qr-code',['qrCodeSimple'=>$qrCodeSimple]);
}
}
Step 6: Create view blade file
Now, create a view file to show the qr code
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 8 qr code generator</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<style>
.jsc{
justify-content: center;
}
</style>
</head>
<body class="antialiased">
<div class="container">
<div class="readersack">
<div class="container">
<div class="row">
</div>
<div class="row jsc">
<!-- show qr code here -->
{!! $qrCodeSimple!!}
</div>
</div>
</div>
</div>
</body>
</html>
Now, open the browser and output will be
Generating Laravel qr code with color
We can change the of qr code by simply adding few lines of code to it
$qrCodeSimple = \QrCode::size(300)->backgroundColor(255,55,0)->generate('ReaderStacks');
//QrCode::color(255, 0, 0); // Red QrCode
//QrCode::color(255, 0, 0, 25); //Red QrCode with 25% transparency
Generating Qr code with Text
QrCode::SMS('97728177671', 'Your message is here');
Generating Qr code with Phone Number
QrCode::phoneNumber('97728177671');
Generating Qr code with Email
QrCode::size(300)->email('admin@readerstacks.com', 'Welcome to readerstacks.');
Generating Qr code in a png image and save to storage
QrCode::size(300)->->format('png')
->generate('readerstacks.com', public_path('images/qrcode.png'));
Generating Qr code with URL
QrCode::size(300)->generate('https://readerstacks.com');
You can check complete list
Usage | Prefix | Example |
---|---|---|
Website URL | http:// | http://www.simplesoftware.io |
Secured URL | https:// | https://www.simplesoftware.io |
E-mail Address | mailto: | support@simplesoftware.io |
Phone Number | tel: | tel:555-555-5555 |
Text (SMS) | sms: | sms:555-555-5555 |
Text (SMS) With Pretyped Message | sms: | sms::I am a pretyped message |
Text (SMS) With Pretyped Message and Number | sms: | sms:555-555-5555:I am a pretyped message |
Geo Address | geo: | geo:-78.400364,-85.916993 |
MeCard | mecard: | MECARD:Simple, Software;Some Address, Somewhere, 20430;TEL:555-555-5555;EMAIL:support@simplesoftware.io; |
VCard | BEGIN:VCARD | See Examples |
Wifi | wifi: | wifi:WEP/WPA;SSID;PSK;Hidden(True/False) |