Generate QR Code Laravel 8 Example

Share this Article
Reading Time: 4 minutes
40 Views

Inside this article we will learn to generate QR code in Laravel 8. There are very basics to create QR code in laravel 8. QR code generate with the help of composer package in Laravel 8.

Let’s get started.


Installation of Laravel 8 Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel 8 already installed at system.


Install simple-qrcode Package

There are several composer packages available which provides support for QR Code. So here by using simple-qrcode Package, we will see working with QR code in Laravel 8.

More about package, here is the link you can find Click here.

Go to your project terminal and install this package.

$ composer require simplesoftwareio/simple-qrcode

When you install this is the screen you should get.

Now open /config/app.php file and add service providers and aliases.

# Add this to providers array
'providers' => [
    ....
    SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class

],

# Add this to aliases array
'aliases' => [
    ....
    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
],

Configure Application Route

Let’s create a route.

Open up the file /routes/web.php

use SimpleSoftwareIO\QrCode\Facades\QrCode;

Route::get('generate-qr-code', function () {
  
    QrCode::size(500)
            ->format('png')
            ->generate('Online Web Tutor', public_path('images/owt.png'));
    
  return view('generate-qr-code');
    
});
  • Import this package to web.php file use SimpleSoftwareIO\QrCode\Facades\QrCode;
  • Using QrCode Methods to generate QR code.
  • Generated QR code code will be saved inside /public/images/ directory.

Create Blade Template File

While creating route we have called generate-qr-code template. So we need to create generate-qr-code.blade.php file inside /resources/views

Open up the blade template file write this following piece of code.

<!DOCTYPE html>
<html>
<head>
    <title>How to Generate QR Code in Laravel 8 - Online Web Tutor</title>
    <style>
      .text-center{
          text-align: center;
      }
    </style>
</head>
<body>
    
<div class="visible-print text-center">
    <h1>Laravel 8 - QR Code Generator Example</h1>
     
    {!! QrCode::size(250)->generate('Online Web Tutor'); !!}
     
    <p>Simple Basic Example by onlinewebtutorblog.com</p>
</div>
    
</body>
</html>

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL- http://127.0.0.1:8000/generate-qr-code

We hope this article helped you to learn about Generate QR Code Laravel 8 in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here

2 thoughts on “Generate QR Code Laravel 8 Example”

  1. BaconQrCode\Exception\RuntimeException
    You need to install the imagick extension to use this back end

    Tanks for this tutorial.

    How can i resolve?

    Reply
    • Hi, you need to run these commands if you are a linux user –
      1. Installation – sudo apt-get install php-imagick
      2. Verification – php -m | grep imagick
      3. Restart Apache – sudo service apache2 restart

      Reply

Leave a Comment