Generate & Scan a QR code in Laravel

You can use the “bacon/bacon-qr-code” package. Here are the steps to install and use it

Install the package via Composer
composer require bacon/bacon-qr-code
In your controller or wherever you want to generate the QR code, use the following code
use BaconQrCode\Renderer\Image\Png;

use BaconQrCode\Writer;

public function generateQRCode($data)

{

    // Generate QR code

    $renderer = new Png();

    $renderer->setHeight(250);

    $renderer->setWidth(250);

    $writer = new Writer($renderer);

    $qrCode = $writer->writeString($data);

    // Return response

    return response($qrCode, 200, [

        'Content-Type' => 'image/png',

        'Content-Disposition' => 'inline; filename="qrcode.png"'

    ]);

}
Call the “generateQRCode” method passing in the data you want to encode into the QR code. For example
public function showQRCode()

{

    $data = 'https://example.com';

    return $this->generateQRCode($data);

}
To scan a QR code in Laravel, you can use the “zxing” package. Here are the steps to install and use it
Install the package via Composer
composer require zxing/zxing
In your controller or wherever you want to scan the QR code, use the following code
use Zxing\QrReader;

public function scanQRCode($imagePath)

{

    // Scan QR code

    $qr = new QrReader($imagePath);

    $text = $qr->text();

    // Return scanned data

    return $text;

}
Call the “scanQRCode” method passing in the path to the image of the QR code. For example
public function processQRCode(Request $request)
{
    $imagePath = $request->file('qrcode')->getPathname();
    $data = $this->scanQRCode($imagePath);
    // Do something with the scanned data
}

Note that you will need to have the appropriate image processing libraries installed on your server for the “zxing” package to work. Note that you will need to have the appropriate image processing libraries installed on your server for the “zxing” package to work.