Laravel 11 Generate QR Code Example Tutorial

09-Apr-2024

.

Admin

Laravel 11 Generate QR Code Example Tutorial

Hi, Dev

In this tutorial, I'll guide you through the process of creating a QR code within a Laravel 11 application. Our aim is to effortlessly generate and store a QR code for a specific link.

We'll leverage the functionality of the simplesoftwareio/simple-qrcode composer package to create QR codes in Laravel 11. This package offers various methods for generating QR codes, including for links, phone numbers, emails, and downloads. Let's delve into examples for each one step by step.

Step for Laravel 11 Generate QR Code Example


Step 1: Install Laravel 11

Step 2: Step 2: Install simplesoftwareio/simple-qrcode

Step 3: Create Route

1: Laravel Generate QR Code for Link Example

2: Laravel Generate QR Code and Save Example

3: Laravel Generate QR Code with Color Example

4: Laravel Generate QR Code with Image Example

5: Laravel Generate Email QR Code Example

6: Laravel Generate Phone QR Code Example

7: Laravel Generate SMS QR Code Example

8: Laravel Generate QR Code in Blade File Example

Let's see the below steps, and you can generate QR code in your Laravel 11 projects as well.

Step:1 Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Install simplesoftwareio/simple-qrcode

composer require simplesoftwareio/simple-qrcodel

Create Route

In this step, we will add routes and a controller file. So first, add the below route in your routes.php file.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\QRCodeController;

Route::get('qr-code', [QRCodeController::class, 'index']);

1: Laravel Generate QR Code for Link Example

We will use the `size()` and `generate()` methods to create a QR code for the link. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

return QrCode::size(300)->generate('https://www.nicesnippets.com');

}

}

Output:

laravel-qr-code-1-1

2: Laravel Generate QR Code and Save Example

We will use the `size()` and `generate()` methods to create a QR code and save it. You need to create a "qrcode" folder in the public folder. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$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

We will use the `size()`, `generate()` and `backgroundColor()` methods to create a QR code with background color. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

return QrCode::size(300)

->backgroundColor(255,55,0)

->generate('A simple example of QR code');

}

}

Output:

laravel-qr-code-2

4: Laravel Generate QR Code with Image Example

We will use the `size()`, `generate()`, and `merge()` methods to create a QR code with an image. You need to provide the path of the image. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$image = QrCode::format('png')

->merge(public_path('images/YOUR_IMAGE_NAME.png'), 0.5, true)

->size(500)

->errorCorrection('H')

->generate('A simple example of QR code!');

return response($image)->header('Content-type','image/png');

}

}

Output:

laravel-qr-code-3

5: Laravel Generate Email QR Code Example

We will use the `size()`, and `email()` methods to create a QR code with for email. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

return QrCode::size(500)

->email('admin@nicesnippets.com', 'Welcome to NiceSnippets.com!', 'This is !.');

}

}

6: Laravel Generate Phone QR Code Example

We will use the `size()`, and `phoneNumber()` methods to create a QR code with for phone number. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

return QrCode::phoneNumber('111-222-6666');

}

}

7: Laravel Generate SMS QR Code Example

We will use the `size()`, and `SMS()` methods to create a QR code with for sending sms. You can see the controller file code:

app/Http/Controllers/QRCodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

QrCode::SMS('111-222-6666', 'Body of the message');

}

}

8: Laravel Generate QR Code in Blade File Example

Here, You can generate OR code in 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>

I hope it can help you...

#Laravel 11