Laravel 8 QR Code Generator Tutorial With Example
In this article, we will discuss how to create QR code using Laravel 8(Laravel 8 QR Code Generator Tutorial With Example). here we are using “simplesoftwareio/simple-qrcode” package to generate QR code in laravel.
We can easily generate simple QR code, QR code, image QR code, text QR code using the laravel. so you can see below example tutorial.
Overview
Step 1: Install Laravel
Step 2: Setting Database Configuration
Step 3: Install QR Code Package
Step 4: Add providers and aliases
Step 5: Create Route
Step 6: Create Controller
Step 7: Create Blade View
Step 8: Run Our Laravel Application
Step 1: Install Laravel
We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel8_qr_code |
Step 2: Setting Database Configuration
After the complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel8_qr_code) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root) |
Step 3: Install QR Code Package
Now, We will install “simplesoftwareio/simple-qrcode” package using the below command.
1 | composer require simplesoftwareio/simple-qrcode |
Step 4: Add providers and aliases
We will add below providers and aliases in the “config/app.php” file.
1 2 3 4 5 6 7 8 | 'providers' => [ .... SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class ], 'aliases' => [ .... 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class ] |
Step 5: Create Route
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php use App\Http\Controllers\QrCodeController; /* |-------------------------------------------------------------------------- | 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('/', function () { // return view('welcome'); }); Route::get('/qr-code', [QrCodeController::class, 'index'])->name('qr.code.index'); ?> |
Step 6: Create Controller
Here below command help to create the controller.
1 | php artisan make:controller QrCodeController |
app/Http/Controllers/QrCodeController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use QrCode; class QrCodeController extends Controller { public function index() { return view('qrcode'); } } |
Step 7: Create Blade View
So finally, first we will create the qrcode.blade.php file and then paste the following code.
resources/views/qrcode.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html> <html> <head> <title>Laravel 8 QR Code Generator Example Tutorial - XpertPhp</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row text-center mt-5"> <div class="col-md-6"> <h4>Simple Qr Code</h4> {!! QrCode::size(250)->generate('XpertPhp') !!} </div> <div class="col-md-6"> <h4>Color Qr Code</h4> {!! QrCode::size(250)->backgroundColor(255,55,0)->generate('XpertPhp') !!} </div> </div> </div> </body> </html> |
Step 8: Run Our Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/qr-code |
Read Also
Laravel 8 Share Social Media Button Example
Laravel 8 Dynamic Dependent Dropdown Using Jquery Ajax
Laravel 8 Vue JS Owl Carousel Slider Example Tutorial