QR Codes in Laravel: A Complete Guide.

QR Codes in Laravel: A Complete Guide.

QR-code is 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 smart phone. Let me show you how you can generate different types of QR codes in Laravel.

QR codes in Laravel:

I set out to research more on generating QR codes in Laravel and I found that there aren’t many packages that could do this but there is one which is really good. simple-qrcode is a Laravel package which is actually a wrapper around the `BaconQrCode` package for PHP.

Install the package:

  • Get the simple-qrcode composer package into your Laravel project by running this in your project directory:
    $ composer require simplesoftwareio/simple-qrcode
    • NOTE: If your Laravel version is 5.4 or less:
      •  Add `SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class` to the `providers` array in your config/app.php.
      • Add the alias 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class to the aliases array in config/app.php.
  • Reload composer auto-load cache:
    $ composer dumpautoload

     

Basic usage:

  • You can quickly whip up a QR code which contains your specified string using QrCode::generate("string") method.
  • Add a qr-code route to your `routes/web.php` routes file.
    Route::get('qr-code', function () {
        return QrCode::size(500)->generate('Welcome to kerneldev.com!');
    });
    • size() method can be used to specify the size of the qr code. By default the size is quite small so we will specify as size of 500.
  •  Visit the route and you are able to see the QR code (Ex, localhost:8000/qr-code). Scan the QR code and you will see the text. 
  • You can also display the QR code in Laravel’s Blade template:
    {!! QrCode::generate('Welcome to kerneldev.com!'); !!}
    

     

Changing the Color of the QR Code:

  • You can also set your own colour for the QR code pattern and the background using color(red, green, blue)and backgroundColor(red, green, blue)methods. You can find the RGB colour codes here.
  • Route::get('qr-code', function () { 
        return QrCode::backgroundColor(255, 255, 0)->color(255, 0, 127)
                       ->size(500)->generate('Welcome to kerneldev.com!'); 
    });

  • NOTE: Many QR code readers have difficulty in reading a coloured QR code hence I suggest you to stick with the non-coloured version if you want your QR code to work with most scanners.
  • You can also change the margin around the QR code by using the margin(5) method. Lower the value lesser the margin.

Placing an image inside the QR code:

  • You can also place an image in the centre of your QR code using the merge('filename.png') method and this function can only accept .png files and you also need to format the response back to png .
    Route::get('qr-code', function () {
            $pngImage = QrCode::format('png')->merge('ss.png', 0.3, true)
                            ->size(500)->errorCorrection('H')
                            ->generate('Welcome to kerneldev.com!');
    
            return response($pngImage)->header('Content-type','image/png');
    });

  • You can also wrap the image in an `<img>` tag in your blade template like this:
    <img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->merge('ss.png', 0.3, true)
                            ->size(500)->errorCorrection('H')
                            ->generate('Welcome to kerneldev.com!')) !!} ">
  • Note that by default, the `merge()` function looks for the file specified in public directory therefore be sure to place your png image there.

Encoding special data in your QR code:

You can encode QR codes in different formats so that most QR code readers will be able to suggest actions based on the QR code encoding type.

  • E-mail:

    • Automatically fill-in email address, subject and body of the email.
    • Syntax:
      QrCode::email($to_address, $subject, $body);
      
    • Example:
      //Specify email address, subject and the body
      QrCode::email('[email protected]', 'Thank you for the QR code tutorial.', 'This was awesome!.');
      
      //Specify subject and the body, let user enter the to_address
      QrCode::email(null, 'Hi  there.', 'This is the message body.');
      
      //Specify just email address
      QrCode::email('[email protected]');
      
  • Phone Number:

    • Open up the phone app and dial the specified phone number.
    • Syntax:
      QrCode::phoneNumber($number);
      
    • Example:
      QrCode::phoneNumber('776-004-1698');
      
  • SMS (Text Messages):

    • Open up the messaging app and fill-in phone number or the message body.
    • Syntax:
      QrCode::SMS($number, $message);
      
    • Example:
      //Specify just the phone number to send the message to
      QrCode::SMS('555-555-5555');
      
      //Specify phone number as well as the message
      QrCode::SMS('555-555-5555', 'Body of the message');
  • Geo co-ordinates:

    • Display the specified location in a map application.
    • Syntax:
      QrCode::geo($latitude, $longitude);
    • Example:
      QrCode::geo(13.3499, 74.798059);

       

It’s that easy to implement QR codes in Laravel! So what do you think? Please share your thoughts and ask any questions you might have in the comment section below and If you are a web/ Laravel developer be sure to checkout my other tutorials related to Web development and Laravel.

P.S:

You can also consider making a small donation to support me. Your donation will directly contribute to the running cost of this website and hopefully my college too 🙂

Paypal: https://www.paypal.me/sapneshnaik  |  UPI: [email protected]

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
36 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
Hamza Masood
Hamza Masood
1 year ago

kindly tell something about creating laravel own qr code reader

umair
umair
1 year ago
Reply to  Sapnesh Naik

that means QR code scanner

umair
umair
1 year ago

Please tell how to scan QR code in laravel

umair
umair
1 year ago

how it make qr code that we get from database, because here we are giving manually values i routes

umair
umair
1 year ago
Reply to  Sapnesh Naik

and how can we increase the size of barcode in blade templating engine, i cleared about routes, but tell how can we increase size of barcode view(blade templating)

Vinay A N
Vinay A N
1 year ago

I need to add the QR code in mail body.

In laravel blade view I am able to see the QR code. If I send as mail I m not getting the QR code.

size(250)->generate($qrText))!!} “>

Mukhami
Mukhami
1 year ago
Reply to  Sapnesh Naik

were you able to find out how to do this?

Evanson Mbuthia
Evanson Mbuthia
1 year ago

how am i able to print the qrcode or make it printable instead of displaying it on browser

mikhael
mikhael
1 year ago

how to set the value of qr code using jquery?

Deep
Deep
1 year ago

How to place multiple QRCodes in blade file. Because when i placed multiple QRCodes in blade file then only first has executed not others.

Aman Babbar
Aman Babbar
1 year ago

Hi
i am using lumen 5.5 but getting error Class ‘QrCode’ not found. i register serviceprovider and alias in both bootstrap/app.php file or AppServiceProvider.php. please help me asap.

rns
rns
1 year ago

This worked perfectly the first time through. Thank you!!

Dani
Dani
1 year ago

how to save to storage ?

amal
amal
1 year ago
Reply to  Sapnesh Naik

where i use that in the controller or blade!!!

amal
amal
1 year ago

how can i save qrcode in my database for exemple when i create a new membre he’s data and he’s qrcode saved in db

matias
matias
1 year ago

como genero qr dentro de un componente .vue, es decir tengo un controlador articulos y un componente articulo.vue e ahi donde genero la vista y quiero generar codigos en articulo.vue pero no me genera solo me sale el error “Failed to load resource: net::ERR_INVALID_URL” espero haberme echo entender…

Shijin
Shijin
1 year ago

Hi Sapnesh, if there is any way to hide some datas from qr code. is this possible?

sobia d
sobia d
1 year ago

How to pass multiple parameter to generate multiple items in qrcode.I want to pass $name,$email,$mobile in generate function.How can we do this.

Hidayah
Hidayah
1 year ago

How to generate QR code with our own data entry and download it into computer drive?

Daniel Kin
Daniel Kin
1 year ago

This package is not online anymore. :/

Tran Tuan Ngoc
1 year ago

Thanks for your blog. Please help me. I want to save QRCode with PNG format and i want to insert some text under the QRCode. How can i do that?

Rashed
Rashed
1 year ago

Is there any package in laravel, by that i can upload an image with QRcode and it will decoded the qrcode and give me the data of that qrcode?

JiiJii
JiiJii
5 months ago

Hello, I’m really interested in creating a QR Code Scanner in Laravel. Do you have some Tutorials about this?

Lincy Anish
Lincy Anish
4 months ago

how can i add some more data like for example for a product its model,name etc to qr code

Lincy Anish
Lincy Anish
3 months ago
Reply to  Sapnesh Naik

Ok Sapnesh,I want to send products name,its color,how can i send this can u plz send me the code

Jon Kraja
Jon Kraja
3 months ago

NIce package.
How can we move the logo from the middle position to the bottom or top position or outside the QR Code .

Thank you in advance

Mano
Mano
3 months ago

How to color QRcode on the corner box of three sides? Not coloring the entire QR but only the corner small boxes.

NIHALA
NIHALA
2 months ago

i tested the same code in controller but getting some code as output instead of displaying qr code…why?