Sling Academy
Home/PHP/How to Generate QR Codes in Laravel (the right way)

How to Generate QR Codes in Laravel (the right way)

Last updated: February 05, 2024

Instroduction

In modern web development, QR codes serve as a bridge between the online and offline worlds, allowing users to access web content by simply scanning a code with their smartphones. Laravel, a popular PHP framework, provides an easy yet powerful way to generate QR codes for your applications. In this tutorial, you will learn how to generate QR codes in Laravel, with practical examples and code snippets to get you started.

Prerequisites

  • A running installation of Laravel. This guide assumes you have Laravel 8.x installed, but the principles should apply to other versions with minor adjustments.
  • Basic knowledge of Laravel’s structure and command-line interface.
  • Composer installed on your machine.
  • An installed version of PHP (7.3 or higher recommended).

Step-by-Step Instructions

Step #1 – Setting Up Your Environment

First, ensure your Laravel project is set up and running. If you need help setting up a new Laravel project, see the following articles:

Step #2 – Installing the QR Code Package

Laravel doesn’t include built-in QR code generation capabilities. However, thanks to its vibrant ecosystem, there are several packages available. For this tutorial, we’ll use the simple-qrcode package by Simplesoftware.io, which is easy to install and use.

composer require simplesoftwareio/simple-qrcode

Step #3 – Generating Your First QR Code

After installing the package, you can immediately start generating QR codes. Here’s a simple example that generates a QR code with a URL:

use SimpleSoftwareIO\QrCode\Facades\QrCode;

Route::get('/qrcode', function () {
    return QrCode::size(200)->generate('https://example.com');
});

This will create a QR code linking to https://example.com when you visit /qrcode on your Laravel application.

Step #4 – Customizing the QR Code

The simple-qrcode package offers various customization options, such as changing the size, color, and adding a logo to the center of the QR code. The following examples show some customization possibilities:

// Change size and color
Route::get('/custom-qrcode', function () {
    return QrCode::size(300)->color(255,0,0)->generate('https://example.com');
});

// Add a logo
Route::get('/qrcode-with-logo', function () {
    return QrCode::format('png')->size(500)->merge('/path/to/logo.png', .3, true)->generate('Welcome to Laravel!');
});

Step #5 – Storing QR Codes as Images

Sometimes, you might want to store the generated QR codes as files. This is easily achieved with the simple-qrcode package. Here’s how you can save a QR code to your storage:


QrCode::size(200)->generate('https://example.com', storage_path('app/public/qrcode.png'));

Ensure that the specified directory exists or create it beforehand. This method is useful for generating QR codes that can be reused or sent in emails.

Conclusion

In this tutorial, you’ve learned how to generate and customize QR codes in Laravel using the simple-qrcode package. This functionality can enhance user experience in a variety of applications, from ticketing systems to physical product verification and beyond. Laravel’s package ecosystem makes it straightforward to incorporate such features, demonstrating once again the framework’s flexibility and capability. Happy coding!

Next Article: Subdomain Routing in Laravel: A Developer’s Guide

Previous Article: Laravel: How to constrain routes with regular expressions

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array