Welcome guys, In this article Here, we will know how to create a QR in laravel. Making aQR in Laravel is simple and easy. if you have question about laravel QR generate then i will give simple example with solution.
Whats is QR ?
A QR code (Quick Response code) is a type of two-dimensional matrix barcode, invented in 1994, by Japanese company Denso Wave for labelling automobile parts. A barcode is a machine-readable optical image that contains information specific to the labelled item. In practice, QR codes contain data for a locator, an identifier, and a website visitor tracking. To efficiently store data
QR codes use four standardized modes of encoding
- numeric
- alphanumeric
- byte or binary etc.
Step 1: Building a New Application
You can ignore this step if you already have an application downloaded or else, run the following command to create a brand new laravel project.
composer create-project laravel/laravel {directory} 10.0 --prefer-dist
After downloaded new application or already installed then moved our application directory run the following command or using your own method
cd {directory}
STEP 2: Run the project
php artisan serve
Visit the link to check the project running. Here we will go to link http://127.0.0.1:8000 and view the default homepage of Laravel.
STEP 3: Configure Database Connection
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=<DATABASE NAME>
DB_USERNAME=<DATABASE USERNAME>
DB_PASSWORD=<DATABASE PASSWORD>
STEP 4: 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
STEP 5: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.
'providers' => [
....
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
],
'aliases' => [
....
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
]
STEP 6: Create a Controller to make function
Controller is the entity where we write the backend logic for our application. Controllers are saved in app/Http/Controllers . For this application we will make a function index in QrController which will return qrcode
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 QrCode::generate(
'http://example.com',
);
}
}
STEP 7: Create route for view qrcode
All Laravel web routes are defined inside your web.php files, which are located in the routes directory. These files are automatically loaded by the framework. The routes
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QrController;
Route::get('/qrcode', [QrController::class, 'index']);
How to Customising the Generated QR Code ?
Alright so how about something a little more interesting ? A splash of colour? Let's use a couple of methods that allow us to change the background colour, foreground colour, and margin.
public function index()
{
return QrCode::size(200)
->backgroundColor(255, 255, 0)
->color(0, 0, 255)
->margin(1)
->generate(
'http://example.com',
);
}
We can actually modify the style and eye (the three corners) of the QR code. Try this out:
public function index()
{
return QrCode::size(200)
->style('dot')
->eye('circle')
->color(0, 0, 255)
->margin(1)
->generate(
'http://example.com',
);
}
Put logo on QR code
To overlay an image in a QR code - we'll need to change the way we do a few things in qrcode config.
To prepare for this I have dropped an image (logo.jpg) in my storage/app folder. Then by calling ->merge($filepath), simple-qrcode will load that image and overlay it onto the QR code config.
If you want to pull in the image data yourself, you can replace merge with mergeString. Eg: ->mergeString(Storage::get($path)). Merging an image is only supported with png QR codes, so we need to specify the format on this, too.
public function index(){
$data = QrCode::size(512)
->format('png')
->merge('/storage/app/logo.png')
->errorCorrection('M')
->generate(
'http://example.com',
);
return response($data)
->header('Content-type', 'image/png');
}
Downloading Generated QR Codes
To return a download response, you can either store the QR code as a file and return the path with return response()->download($path); or stream the contents of the QR code without using the filesystem.
An example of using streamDownload to do this:
public function download()
{
return response()->streamDownload(
function () {
echo QrCode::size(200)
->format('png')
->generate('http://example.com');
},
'qr-code.png',
[
'Content-Type' => 'image/png',
]
);
}
Generating QR Codes in Blade
Since we've been using a Facade all along, generating a QR code in Blade is as simple as repeating the above steps. Call the facade, customise as required, and call generate.
{!! QrCode::size(300)->generate('http://example.com') !!}
Or another approach, encode the data within an image tag manually. For example, if you need to change the image type for whatever reason.
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->size(300)->generate('https://google.com')) !!} ">
QR Code Data Types
Open a blank email addressed to "[email protected]".
QrCode::size(200)->email('[email protected]');
Open an email with a predefined subject and body.
QrCode::size(200)->email('[email protected]', 'Hello K.K ADIL', 'This is a test email.');
Phone Number
QrCode::size(200)->phoneNumber('91XX-XXX-XXX-X00');
SMS Text Message
Send an SMS text message to 91-XXX-XXX-X00 with a prewritten message, "Hi!".
QrCode::size(200)->SMS('XXX-XXX-XXX-X00', 'Hi!');
Wi-Fi
Share Wi-Fi credentials for your visitors.
QrCode::size(200)->wiFi([
'encryption' => 'WPA/WEP',
'ssid' => 'SSID of the network',
'password' => 'Password of the network',
'hidden' => 'Whether the network is a hidden SSID or not.'
]);
Geolocation
Share a location by providing a latitude and longitude.
QrCode::size(200)->geo(51.378698, -0.110897);