QR (Quick Response) code is scannable black and white square box that stores data.
They are commonly used to store information, redirect users to a website, download files, payment, etc.
In this tutorial, I show how you can generate QR code using simple qrcode package in Laravel 8.
Contents
1. Install Package
Install the package using composer –
composer require simplesoftwareio/simple-qrcode
2. Update app.php
- Open
config/app.php
file. - Add the following
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class
in'providers'
–
- Add the following
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
in'aliases'
–
3. Route
- Open
routes/web.php
file. - Define a routes –
- / – Load index view.
Completed Code
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PageController; Route::get('/', [PageController::class, 'index']);
4. Controller
- Create
PagesController
Controller.
- Open
app/Http/Controllers/PagesController.php
file. - Import QrCode package –
use SimpleSoftwareIO\QrCode\Facades\QrCode;
- Create a method –
- index() – Generating a simple QR code which holds some text and assign to
$data['qrcode']
.
- index() – Generating a simple QR code which holds some text and assign to
Generate QR code with text and store in images/
folder for download. Load index
view and pass $data
.
Completed Code
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use SimpleSoftwareIO\QrCode\Facades\QrCode; class PageController extends Controller { public function index(){ // QR code with text $data['qrcode'] = QrCode::generate('Welcome to Makitweb'); // Store QR code for download QrCode::generate('Welcome to Makitweb', public_path('images/qrcode.svg') ); return view('index',$data); } }
5. View
Create index.blade.php
file in resources/views/
folder.
Display QR code generated in the controller –
{!! $qrcode !!}QR code with text –
{!! QrCode::generate('Welcome to Makitweb'); !!}Pass text in generate()
.
QR code with URL –
{!! QrCode::generate('https://makitweb.com'); !!}Pass URL in generate()
.
Change QR code color –
QrCode::color(224, 224, 224)->backgroundColor(102, 0, 204)->generate('Welcome to Makitweb');color() – Pass RGB color code to change QR color.
backgroundColor() – Pass RGB color code to change QR background color.
Change size –
QrCode::size(200)->generate('Welcome to Makitweb');By default QR code size is 100px, to change it use size()
.
Change format –
QrCode::format('png')->generate('Welcome to Makitweb');Default format of QR code is SVG, to change pass png
to format()
.
NOTE – Make sure
imagick
extension is installed otherwise it will display error.
Download QR code –
Saved QR code to images/qrcode.svg
using –
Pass file path in anchor tag –
<a href="{{ asset('images/qrcode.svg') }}" download>Download</a>Completed Code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>How to Generate QR Code Using Simple QRcode In Laravel 8</title> </head> <body> <table border="1" style="border-collapse: collapse;"> <tr> <td> QR with text <br> Generated from controller </td> <td> {!! $qrcode !!} </td> </tr> <tr> <td> QR with text </td> <td> {!! QrCode::generate('Welcome to Makitweb'); !!} </td> </tr> <tr> <td> QR with URL </td> <td> {!! QrCode::generate('https://makitweb.com'); !!} </td> </tr> <tr> <td> Change QR color </td> <td> {!! QrCode::color(224, 224, 224)->backgroundColor(102, 0, 204)->generate('Welcome to Makitweb'); !!} </td> </tr> <tr> <td> Change default size to 200px </td> <td> {!! QrCode::size(200)->generate('Welcome to Makitweb'); !!} </td> </tr> <tr> <td> PNG format </td> <td> {!! QrCode::format('png')->generate('Welcome to Makitweb'); !!} </td> </tr> <tr> <td>Download QR code</td> <td> <a href="{{ asset('images/qrcode.svg') }}" download>Download</a> </td> </tr> </table> </body> </html>
0 comments: