The main purpose of this blog is to create and display barcode or QR code based on user input. In below examples, we are going to use milon/barcode library.
For your better understanding we have divided program into some parts.
let's assume you have Laravel application. If you don't have any application then create new application try further steps into it.
Creating and displaying barcode or QR code in your application, we are going to use milon/barcode library. This library contains all functionality to create and display all kinds of barcodes.
So let's install milon/barcode into our Laravel application using below command :
composer require milon/barcode
This command will download related files to your application.
Before using this package, we need to register it into your application config file. So you can access it whenever required. To register open app.php file and add those lines :
config\app.php
<?php
return [
'providers' => [
...
Milon\Barcode\BarcodeServiceProvider::class,
],
'aliases' => [
...
'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
]
];
Let's create new controller which will handle your barcode methods. you will need this controller to load blade file. You can use existing controller if required. For creating new controller enter below command into your terminal :
php artisan make:controller BarcodeController
It will create Barcode controller at app\Http\Controllers\ location.
Now let's modify our controller which will take user input and pass it to view. For that make following changes into your controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Symfony\Component\Console\Input\Input;
class BarcodeController extends Controller
{
public function showBarcode(Request $request)
{
$user_input = $request->input('barcode');
return view('show_barcode', compact('user_input'));
}
}
Barcode Controller has only one method which will get request input and pass this input to QR code display page.
Let's create route which will interacts with the barcode controller and provides a link to view. thus update the routes/web.php file.
<?php
use App\Http\Controllers\BarcodeController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::post('/generate-barcode',[BarcodeController::class, 'showBarcode'])->name('barcode.show');
Whenever user hits this route with post method then it will call showBarcode methods.
In this example we will create QR code on user input so you need two views. One will take user input and second will display QR code to user. Here, we will modify default welcome view for user input and create new view for display barcodes. >We can create any type of barcode or QR code using this library. Modify below views :
resources/views/welcome.blade.php :
resources/views/show_barcode.blade.php :
php artisan serve
open below URL into your browser :
http://127.0.0.1:8000
Here, Home page will ask for input and when we submit this input It will display QR code and Barcode with input value. In this view, we have created multiple type of barcodes. So use it according to your requirements.
Ask anything about this examples