How to Generate QR Codes in Laravel 9 with Examples

A QR code (Quick Response Code) is a type of two-dimensional barcode that can be read by a smartphone or other mobile device with a camera. QR codes can store a variety of information, such as URLs, phone numbers, contact information, and even images.

In this tutorial, we will learn how to generate QR codes in Laravel 9 using the simplesoftwareio/simple-qrcode package. This tutorial is perfect for beginners and experienced Laravel developers alike.

Table of Contents
 

  • Step 1: Install Laravel 9 App
  • Step 2: Connecting App to Database
  • Step 3: Install simple-QRcode Package
  • Step 4: Configure Simple QR Code Package
  • Step 5: Add Route
  • Step 6: Run Development Server

Step 1: Install Laravel 9 App

First of all, open the terminal and execute the following command on the terminal to install or download Laravel 9 app:

composer create-project --prefer-dist laravel/laravel Laravel-QR-Code

Step 2: Connecting App to Database

In this step, open .env and configure database details for connecting app to database:

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Step 3: Install simple-QRcode Package

In this step, Execute the following command terminal to install simple-QRcode package for generate different types of QR code generator in laravel 9 app:

composer require simplesoftwareio/simple-qrcode

Step 4: Configure Simple QR Code Package

After successfully install a simple QR code package in the laravel app. Next, open config/app.php file and add service provider and aliases.

//config/app.php
 
'providers' => [
 ….
 SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class
 ],
 
'aliases' => [
 ….
 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
 ],

Step 5: Add Routes

Now, open web.php file and add the following routes into it, which is located inside routes directory:

Simple Qr code :

Add a route and return QR code. Simply add the following code in your web.php file.

Route::get('qrcode', function () {
     return QrCode::size(300)->generate('A basic example of QR code!');
 });

size() function is used to specify the size of QR. When you hit the route /QRcode, you get the QR code as below :

php qr code

QR Code with Color :

Now, you can change the color of QR code. Go to route web.php file and specify this route.

Route::get('qrcode-with-color', function () {
     return \QrCode::size(300)
                     ->backgroundColor(255,55,0)
                     ->generate('A simple example of QR code');
 });

The above code output a QR code shown below :

QR Code 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');


Email QR code :

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('foo@bar.com');

//Fills in the to address, subject, and body of an e-mail.
QrCode::email('foo@bar.com', '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.');

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');


Step 6: Run Development Server

Now, execute the following command on terminal to start development server:

php artisan serve

In this tutorial, we have successfully installed and configure a simple QR code package in laravel application.

#laravel #php  

How to Generate QR Codes in Laravel 9 with Examples
2 Likes3.05 GEEK