Laravel 11 Generate QR Code Example

Laravel 11 generate qr code example; Through this tutorial, i am going to show you how to generate or create simple qr code using simplesoftwareio/simple-qrcode package in Laravel 11 apps.

In this tutorial, i will use simplesoftwareio/simple-qrcode package to generate simple, text, numeric and image qr code in Laravel 11 app.

Laravel 11 Generate QR Code Example

Use the below given simple steps to generate different types of qr code in Laravel 11 apps:

  • Step 1 – Install Laravel 11 Application
  • Step 2 – Database Configuration
  • Step 3 – Installing simplesoftwareio/simple-qrcode
  • Step 4 – Configure simplesoftwareio/simple-qrcode
  • Step 5 – Create QR Code Generate Routes
  • Step 6 – Creating QRCode Controller
  • Step 7 – Run Development Server
  • Step 8 – Run This App on Browser

Step 1 – Install Laravel 11 Application

In step 1, install Laravel 11 new setup. So, open terminal and type the following command to install new Laravel 11 app into your machine:

composer create-project --prefer-dist laravel/laravel LaravelPDFGenerate

Step 2 – Database Configuration

In step 2, configure database with your downloded/installed laravel app. So, you need to find .env file and setup database details as following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Step 3 – Installing simplesoftwareio/simple-qrcode

In step 3, install simplesoftwareio/simple-qrcode package in laravel app via following command:

composer require simplesoftwareio/simple-qrcode

Step 4 – Configure simplesoftwareio/simple-qrcode

In step 4, configure the simplesoftwareio/simple-qrcode package in laravel app. So, Open the providers/config/app.php file and register the simplesoftwareio/simple-qrcode provider and aliases.

'providers' => [
	....
	SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class
],
  
'aliases' => [
	....
	'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
]

Step 5 – Create QR Code Generate Routes

In step 5, add the qr code generation routes in web.php file, which is located inside routes directory:

use App\Http\Controllers\QRCodeController;

Route::get(simple-qr-code', [QRCodeController::class, 'simpleQr'])
Route::get(color-qr-code', [QRCodeController::class, 'colorQr'])
Route::get(image-qr-code', [QRCodeController::class, 'imageQr'])

Step 6 – Creating QRCode Controller

In this step, create generate pdf controller file by using the following command:

php artisan make:controller QRCodeController

Then navigate to app/http/controllers and open QRCodeController.php file. And add the simple qr code generation code into it:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;

use QrCode;
  
class QRCodeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function simpleQr()
    {
       return QrCode::size(300)->generate('A basic example of QR code!');
    }  

    public function colorQr()
    {
              return QrCode::size(300)
                     ->backgroundColor(255,55,0)
                     ->generate('Color QR code example');
    }    

    public function imageQr()
    {
        $image = QrCode::format('png')
                 ->merge('images/laravel.png', 0.5, true)
                 ->size(500)->errorCorrection('H')
                 ->generate('Image qr code example');
        return response($image)->header('Content-type','image/png');
    }
}

Note that, if you want to send qr code in mail, phone and text message in laravel app. So, you can add following method on controller file:

    public function emailQrCode()
    {
        return QrCode::size(500)
                ->email('[email protected]', 'Welcome to Lara Tutorials!', 'This is !.');
    }  

    public function qrCodePhone()
    {
             QrCode::phoneNumber('111-222-6666');
    }    

    public function textQrCode()
    {
      QrCode::SMS('111-222-6666', 'Body of the message');
    }

Step 7 – Run Development Server

In step 7, open command prompt and type the below given command to run development server:

php artisan serve

Step 8 – Run this App On Browser

Now, open your browser and hit the following url on it:

http://127.0.0.1:8000/simple-qr-code
http://127.0.0.1:8000/color-qr-code
http://127.0.0.1:8000/image-qr-code

Recommended Laravel Tutorials

Leave a Comment