QR code generator in Laravel 5 [A complete guide]

Laravel 5 QR Code generator

A machine-readable code consisting of an array of black and white squares, typically used for storing URLs or other information for reading by the camera on a smartphone. QR code is used to take a piece of information.

In this article, we will generate QR code in our laravel application. We will use a simplesoftwareio/simple-qrcode package. It is a wrapper for the laravel framework based on the work of Bacon/BaconQrCode.

Install Package

We will use composer to install the simple-qrcode package. We are using laravel 5.7 version. If you are installing in laravel 5.4 and below, you need to add service provider and alias.

Basic Usage

Now, we are ready to use simple-qrcode in our application. We will start with the basic usage. Using this is dead simple. It’s basic syntax is:

To test it, we will add a route and return QR code. Simply add the following code in your web.php.

size() function is used to specify the size of QR.

When we navigate to the route /qrcode, we get the QR code as below:
Laravel 5 QR Code generator

We can also directly generate the QR code in the blade file as below:

Format

By default, generate method returns a svg image. We can change the format of returned image by following way:

Please note that, format should be called before any method while chaining:

Size

We can change the size of QR code according to our needs.

Color

Similar to size, we can change the color of QR code. To change the color of QR code other than black, we use color method and pass RBG value of color as parameter.

Similarly, we can change the background of QR code as like below:

The above code snippet out a QR code as screenshot below:

Laravel 5 QR Code generator

Note: Most of the scanners find it difficult to read the colored QR code. So, it is best to stick with the Black and White color.

Margin

This package also provides an option to change the margin around the QR code. For this, we should margin method with value as a parameter.

Encoding

It is also possible to provide encoding of the character. The encoding method is used to specify it.

Placing Image

We can also place an image inside the QR code. The merge method is used to place an image. A note to remember is that it only accepts png and also need to format the response back to png.

The above code snippet out a QR code as screenshot below:

Laravel 5 QR Code generator

Encoding sepcial data

With the helper of helpers, we can cause a reader to perform certain action when scanned.

Bitcoin

We can send payment when scanned with the help of this helper function.

Email

We can also automatically fill email, subject and body when scanned.

Geo Location

We can provide longitude and latitude through the QR code and open the location in google map.

Phone Number

This package also helps to dial a number when QR code is scanned.

Text Message

We can prefilled phone number and text messages when scanned the QR code.

Connect Wifi

This helpers makes scannable QrCodes that can connect a phone to a WiFI network.

Conclustion

It’s easy to generate QR code with the help of this package. We see that we can do a lot with this package from generating a simple text QR code to Sending emails. This is a nice easy to use package. You can read it’s documentation on its official website also.

If you love this article, you may also like to read Laravel 5.7 CRUD example from scratch.

If you find this article interesting, please share. If you have any questions or feedback, please drop in the comment section below.