In this article, we will discuss how to Generate QR Code in Laravel 8(Laravel 8 QR Code Generator Tutorial With Example). here we use “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 the below example of laravel generate qr code.

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.

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.

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.

composer require simplesoftwareio/simple-qrcode

Step 4: Add providers and aliases

We will add below providers and aliases in the “config/app.php” file.

'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.

<?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.

php artisan make:controller QrCodeController

app/Http/Controllers/QrCodeController.php

<?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

<!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.

php artisan serve

Now we will run our example using the below Url in the browser.

http://127.0.0.1:8000/qr-code