How to Generate QR Code in Laravel 10
In this post we will give you information about How to Generate QR Code in Laravel 10. Hear we will give you detail about How to Generate QR Code in Laravel 10 And how to use it also give you demo for it if it is necessary.
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 10 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 10 app. It gives a simple QrCode wrapper, which is easy to integrate into laravel.
How to Generate QR Code in Laravel 10
- 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:^9.0 qr-code-example
Note: Laravel 10 requires PHP 8.0 or PHP 8.1.
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>
Checkout This Tool: Open Port Check Tool
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' => [
....
SimpleSoftwareIOQrCodeQrCodeServiceProvider::class,
],
'aliases' => [
....
'QrCode' => SimpleSoftwareIOQrCodeFacadesQrCode::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:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
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 are located inside the routes directory:
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersQrCodeController;
/*
|--------------------------------------------------------------------------
| 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 10</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
<div >
<div >
<div >
<h3>Simple QR Code</h3>
</div>
<div >
{!! QrCode::size(300)->generate('https://onlinecode.org/generate-qr-code-laravel-10') !!}
</div>
</div>
<div >
<div >
<h3>Color QR Code</h3>
</div>
<div >
{!! QrCode::size(300)->backgroundColor(255,90,0)->generate('https://onlinecode.org/generate-qr-code-laravel-10') !!}
</div>
</div>
</div>
</body>
</html>
Helpers For Generate Different QR Codes
BitCoin
This helper generates a scannable bitcoin to send payments.
QrCode::BTC($address, $amount);
//Sends a 0.334BTC payment to the address
QrCode::BTC('bitcoin address', 0.334);
//Sends a 0.334BTC payment to the address with some optional arguments
QrCode::size(500)->BTC('address', 0.0034, [
'label' => 'my label',
'message' => 'my message',
'returnAddress' => 'https://www.returnaddress.com'
]);
This helper generates an e-mail QRcode that is able to fill in the e-mail address, subject, and body:
QrCode::email($to, $subject, $body);
//Fills in the to address
QrCode::email('[email protected]');
//Fills in the to address, subject, and body of an e-mail.
QrCode::email('[email protected]', 'This is the subject.', 'This is the message body.');
//Fills in just the subject and body of an e-mail.
QrCode::email(null, 'This is the subject.', 'This is the message body.');
Geo
This helper generates latitude and longitude that a phone can read and opens the location in Google Maps or a similar app.
QrCode::geo($latitude, $longitude);
QrCode::geo(37.822214, -122.481769);
Phone Number
This helper generates a QR code that can be scanned and then dials a number.
QrCode::phoneNumber($phoneNumber);
QrCode::phoneNumber('555-555-5555');
QrCode::phoneNumber('1-800-Laravel');
SMS (Text Messages)
This helper makes SMS messages that can be prefilled with the send-to address and body of the message:
QrCode::SMS($phoneNumber, $message);
//Creates a text message with the number filled in.
QrCode::SMS('555-555-5555');
//Creates a text message with the number and message filled in.
QrCode::SMS('555-555-5555', 'Body of the message');
WiFi
This helper makes scannable QR codes that can connect a phone to a WiFi network:
QrCode::wiFi([
'encryption' => 'WPA/WEP',
'ssid' => 'SSID of the network',
'password' => 'Password of the network',
'hidden' => 'Whether the network is a hidden SSID or not.'
]);
//Connects to an open WiFi network.
QrCode::wiFi([
'ssid' => 'Network Name',
]);
//Connects to an open, hidden WiFi network.
QrCode::wiFi([
'ssid' => 'Network Name',
'hidden' => 'true'
]);
//Connects to a secured WiFi network.
QrCode::wiFi([
'ssid' => 'Network Name',
'encryption' => 'WPA',
'password' => 'myPassword'
]);
Run Laravel App
Eventually, use the PHP artisan command to start the laravel server, and also use the given URL to view the app.
php artisan serve
Thank you for reading this blog.
Also see: How to Deploy Laravel Application with Nginx on Ubuntu
. .
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.
Hope this code and post will helped you for implement How to Generate QR Code in Laravel 10. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs