Laravel 9 - How to generate Qr Code
Laravel 9 - How to generate Qr Code
In this tutorial, we’ll cover generating QR codes in Laravel 9, which are useful for sharing URLs, product details, and contact information quickly through scanning.
If you're a video person, feel free to skip the post and check out the video instead!
Step # 1 : Create fresh Laravel project or use an existing Laravel project.
Two commands to create fresh Laravel project.
Global Command : laravel new qrcode
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist qrcode
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/qrcode
Run the Laravel Vite development server. Install the required dependencies and start the Vite server for front-end assets.
Command : npm install && npm run dev
Open a new Git Bash window or tab, and navigate to the same project directory to run further Laravel commands.
Step # 3 : Install qrcode package.
Command : composer require simplesoftwareio/simple-qrcode "~1"
Step # 4 : Publish the configuration (Optional)
Command : php artisan vendor:publish --provider="SimpleSoftwareIO\QrCode\QrCodeServiceProvider"
Step # 5 : Create controller.
Command : php artisan make:controller QrCodeController
Make a method named index to return a view.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QrCodeController extends Controller
{
public function index()
{
return view('qrcode');
}
}
Step # 6 : Create a view.
In order to display the Qr Code create a view named qrcode.blade.php.
(resources/views/qrcode.blade.php)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel 9 - Code Shotcut - How to Generate QR Code</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
<div class="container mt-4">
<div class="card">
<div class="card-header text-center">
<h2>SCAN QR CODE TO ACCESS MY CHANNEL - All ABOUT LARAVEL</h2>
</div>
<div class="card-body text-center">
{!! QrCode::size(400)->generate('https://www.youtube.com/@AllAboutLaravel') !!}
</div>
</div>
</div>
</body>
</html>
Step # 7 : Create a route.
Import QrCodeController class
use App\Http\Controllers\QrCodeController;
Create a route
Route::get('/qr', [QrCodeController::class, 'index']);
Step # 8 : It's time to test.
Run the Laravel server.
Command : php artisan serve
Access url 127.0.0.0:8000/qr
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments