DEV Community

Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on

Generate QR code using laravel 9.

Hello Readers,

In this blog post we will see how to create QRCode in laravel 9 using simplesoftwareio/simple-qrcode package.

Firstly we will setup our laravel 9 project by following below steps

  • Step 1 – Install Laravel 9 Application
  • Step 2 – Database Configuration
  • Step 3 – Installing simplesoftwareio/simple-qrcode package
  • Step 4 – Configure simplesoftwareio/simple-qrcode
  • Step 5 – Create QRCode Controller
  • Step 6 – Add QR Code Routes
  • Step 7 – Run This App on Browser

Step 1 - create and install laravel 9 using command

composer create-project --prefer-dist laravel/laravel LaravelPDFGenerate
Enter fullscreen mode Exit fullscreen mode

Step 2 - Configure database

  DB_CONNECTION=mysql
  DB_HOST=127.0.0.1
  DB_PORT=3306
  DB_DATABASE=qr_code_laravel
  DB_USERNAME=root
  DB_PASSWORD=password
Enter fullscreen mode Exit fullscreen mode

Step 3 - Install QRCode package using composer





  composer require simplesoftwareio/simple-qrcode
Enter fullscreen mode Exit fullscreen mode

Step 4 - Configure simplesoftwareio/simple-qrcode package in config/app.php file

Open config/app.php file and add below line in providers.

SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class

and add below line in aliases.

QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class

Step 5 - To create QRCode controller run command

php artisan make:controller QRCode/QRCodeController

Step 6 - Create routes, open web.php file and add routes

  Route::get('qr-code', [QRCodeController::class, 'index']);
  Route::get('qr-code-colored', [QRCodeController::class, 
  'colorQrCodeIndex']);
Enter fullscreen mode Exit fullscreen mode

Now we have successfully setup our project, the next step is to write a code to generate QRCode. 

Open Controller and we add below code

    public function index()
    {
       return QrCode::size(200)->generate('My first QR 
 Code');
    }
    public function colorQrCodeIndex()
    {
      return QrCode::size(100)
               ->backgroundColor(255,255,10)
               ->generate('Example of Colored QR code');
    }
Enter fullscreen mode Exit fullscreen mode
  • size() - is used to define the size of the QR Code
  • generate() method - hold the message, address, price, phone etc for which we are generating QR code.
  • background() - used to give background colour to our QR.




Step 7 - Run the development server by running php artisan serve command and visit the browser to see your QR code.



http://127.0.0.1:8000/qr-code-colored - for coloured QR
http://127.0.0.1:8000/qr-code - for simple QR

Happy Reading…
🦄 ❤️

Top comments (0)