This guide explains how to implement QR code generation in a Laravel application using the popular
simplesoftwareio/simple-qrcode
package.
To generate QR codes, install the
simple-qrcode
package. If not already installed, add it via Composer:
composer require simplesoftwareio/simple-qrcode
In this example we use a URL but any kind of string can be used.
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class ExampleController extends Controller
{
public function store(Request $request) {
// Generate an URL with query parameters for the QR code
$url = 'https://laravel.com/docs/10.x/'
// Generate a QR code containing the URL
$qrCode = QrCode::size(200)->generate($url);
// Return the index view and pass the generated QR code
return view('example_view', compact('qrCode'));
}
}
example_view.blade.php
For viewing the generated QR code we can easily use the double curly brackets.
<div>
{{ $qrCode }}
</div>
With this simple implementation, you can easily create and output a QR code using Laravel.
If you encounter issues while installing the QR code package via Composer, make sure that the GD extension is enabled in your php.ini
file. To do this, change the following line:
;extension=gd
to:
extension=gd
Save the file and restart your web server. After that, the installation of the Composer package should work without any issues.