Home Laravel How To Generator QR Code In Laravel 9?

How To Generator QR Code In Laravel 9?

by Shahriar Sagor

Today now in this post,i will show you how we can generate QR code laravel 9 . Here i will help you to give example of how to generate QR code in laravel 9. So here in this article i will give you a very simple example of how to create an qr code in laravel 9 application. Here i will look at example of how to save and generated QR code in laravel 9 application.

Now in this example, i will generate a QR code by using the simplesoftwareio/simple-qrcode composer package. Here I will give you a very simple and small example of generating QR code also with image, QR code with the color, QR code with SMS, QR code with the email, and QR code in blade file, etc.

So Let’s start and see the bellow step and we can generate QR code in our laravel 9 projects as well.

Install Laravel 9

So now this is optional; however, if you do not have then need to created the laravel application, then we may go ahead and then execute the below command:

composer create-project laravel/laravel example-app
Install simplesoftwareio/simple-qrcode

Now in the first step i will install the simplesoftwareio/simple-qrcode Package that is provides us to generate QR code in laravel 9 application. So, at first need to open our terminal and then run the bellow command:

composer require simplesoftwareio/simple-qrcode
1: Laravel Generate QR Code Example

Here, i will create a simple route for generating the qr code, Then here you will see output:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('qrcode', function () {
  
     return QrCode::size(300)->generate('A basic example of QR code!');
});

2: Laravel Generate QR Code and Save Example

Here, I will create a simple route for generating the qr code:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('qrcode-with-color', function () {
	 $path = public_path('qrcode/'.time().'.png');
  
     return QrCode::size(300)
                     ->generate('A simple example of QR code', $path);
});

3: Laravel Generate QR Code with Color Example

Here, i will create the simple route for generating the qr code:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('qrcode-with-color', function () {
  
     return QrCode::size(300)
                     ->backgroundColor(255,55,0)
                     ->generate('A simple example of QR code');
});
4: Laravel Generate QR Code with Image Example

Here, i will create a simple route for generating qr code with image:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('qrcode-with-image', function () {
  
        $image = QrCode::format('png')
                         ->merge(public_path('images/1643263030.png'), 0.5, true)
                         ->size(500)
                         ->errorCorrection('H')
                         ->generate('A simple example of QR code!');
  
        return response($image)->header('Content-type','image/png');
});

5: Laravel Generate Email QR Code Example

Here, i will create a simple route for generating the qr code with email:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
     
Route::get('qrcode-email', function() {
  
     return QrCode::size(500)
                 ->email('srsagor@codingspoint.com', 'Welcome to ItSolutionStuff.com!', 'This is !.');
 });
6: Laravel Generate Phone QR Code Example

Here, i will generate qr code with any phone number as below:

Code:

QrCode::phoneNumber('01797245860');

7: Laravel Generate SMS QR Code Example

Here, i will generate qr code also with send sms as below:

Code:

       QrCode::SMS('01797245860', 'Body of the message');  
8: Laravel Generate QR Code in Blade File Example

Here, we can also generate QR code in the blade file:

Code:

<div class="visible-print text-center">
    {!! QrCode::size(100)->generate('Demo'); !!}
    <p>Scan me to return to the original page.</p>
</div>

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like