Now days, QR code are essential part of web application. It doesn't matter you are creating E-commerce site, social media, payment site or any other, It helps to provides easy user interface.
In this blog, you will learn how to create QR code for your Laravel application using simplesoftwareio/simple-qrcode Package. This package provides all functionality for almost all kind of QR codes.
You can easily install this package in Laravel using the Composer package. after installation it requires package services to be registered in config/app.php. After that, you can use it as per your requirements.
In this tutorial, you will need to follow these step to create QR code into your application from scratch.
Note : Skip step 1 and 2 if you already have application.
First, open terminal and create new Laravel application using below command :
Let's configure database for our application. If you have don't have any database then create a new one. After creating database open .env file from root directory of your project. if .env file is missing from project then copy content from .env.example and create file. .env file defines many common environment variables
Let's head back to terminal at root directory of project and enter below command :
composer require simplesoftwareio/simple-qrcode
It will take some time to install simplesoftwareio/simple-qrcode package. We will use this package to generate QR codes.
Now, we have installed our package but in order to use it, we need to register it to our application. you can do that by registering it into config\app.php file. so open the file and update the providers and alias array with the given below services.
config\app.php
<?php
return [
'providers' => [
...
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
],
'aliases' => [
...
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
]
];
Let's create a controller which will handle, user request and pass user input to view. For creating new controller enter below command into your terminal :
php artisan make:controller QrCodeController
Now let's modify our controller which will take user input and pass it to view. For that make following changes into your controller :
App\Http\Controllers\QrCodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QrCodeController extends Controller
{
public function showQrCode(Request $request)
{
$user_name = $request->input('name');
return view('qr_code', compact('user_name'));
}
}
Let's create route which will interacts with the QrCodeController and provides a link to view.
routes\web.php
<?php
use App\Http\Controllers\QrCodeController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::post('/show-qr-code',[QrCodeController::class, 'showQrCode'])->name('qr_code.show');
Here, we are planning to display QR Code from user input so we need to 2 views. First will take user input like name and second will display QR code for that name.
We will use default welcome view to get user input. In this blade file, we will remove all content and just add simple form with one input.
resources\view\welcome.blade.php
Let's create new blade file for display qr codes to user. Here, we will bootstrap for styling output.
resources\view\qr_code.blade.php
php artisan serve
open below URL into your browser :
http://127.0.0.1:8000
Ask anything about this examples