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:

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

     

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.

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

     

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.

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

  • You can also wrap the image in an <img> tag in your blade template like this:
  • 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:
    • Example:
  • 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:
    • Example:
  • Geo co-ordinates:

    • Display the specified location in a map application.
    • Syntax:
    • Example:

       

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.

 

8
Leave a Reply

avatar
3 Comment threads
5 Thread replies
3 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Sapnesh NaikumairHamza Masood Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Hamza Masood
Guest
Hamza Masood

kindly tell something about creating laravel own qr code reader

umair
Guest
umair

Please tell how to scan QR code in laravel

umair
Guest
umair

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