Sapnesh Naik
Software Developer, Technical Writer
Blog Post

QR Codes in Laravel: A Complete Guide

September 7, 2018

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 smartphone. 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 that 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!');
    • size() method can be used to specify the size of the QR code. By default, the size is quite small so we will specify a 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.

    QR Code Laravel 7

  • You can also display the QR code in Laravel’s Blade template:

    {!! QrCode::generate('Welcome to!'); !!}

Changing the Color of The QR Code:

  • You can also set your own color for the QR code pattern and the background using color(red, green, blue)and backgroundColor(red, green, blue)methods. You can find the RGB color codes here.

    Route::get('qr-code', function () {
      return QrCode::backgroundColor(255, 255, 0)->color(255, 0, 127)
                     ->size(500)->generate('Welcome to!');

Colored QR Code Laravel

  • 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 center 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)
                      ->generate('Welcome to!');
      return response($pngImage)->header('Content-type','image/png');

    Kerneldev QR Code Laravel

  • You can also wrap the image in an <img> tag in your blade template like this:

    {!! base64_encode(QrCode::format('png')->merge('ss.png', 0.3, true)
                            ->generate('Welcome to!')) !!} ">
  • 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:
    //Specify email address, subject and the body
    QrCode::email('', '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
  • Phone Number:

    • Open up the phone app and dial the specified phone number.
    • Syntax:
    • Example:
  • 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
    //Specify phone number as well as the message
    QrCode::SMS('555-555-5555', 'Body of the message');
  • Geo Coordinates:

    • 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.