In this Laravel Barcode generator example guide, you will learn the best and easy way on how to create barcode generator functionality in the laravel application with the help of the milon barcode generator library.
This guide will help you enumerate bit by bit every step, which will help you create the barcode generator module in the laravel application and that too from the absolute beginning.
The milon/barcode package allows you to create barcode in laravel without even putting intense effort.
In this tutorial, we will learn the secrets to build and display a variety of barcodes, so don’t worry, this post will surely teach you how and every step of the way.
Laravel 9 Barcode Generator Example
- Step 1: Download Laravel Project
- Step 2: Connect to Database
- Step 3: Add Barcode Generator Library
- Step 4: Create New Controller
- Step 5: Register Route
- Step 6: Implement Barcode Generator in View
- Step 7: Run App in Browser
Download Laravel Project
Create laravel application anyhow make sure you have Composer installed on your system:
composer create-project laravel/laravel --prefer-dist laravel-ecommerce
Enter inside the project folder.
cd laravel-ecommerce
Connect to Database
Connect laravel application to the database for that you have to insert the database details into the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_username
DB_PASSWORD=db_password
Add Barcode Generator Library
Barcode package is easy to install using composer require command, make sure to append milon/barcode on the terminal and execute command to install the package.
composer require milon/barcode
To use the package, you have to update a couple of services and classes of the library.
Go to config/app.php and insert the BarcodeServiceProvider in the providers update the aliases array.
return [
'providers' => [
....
Milon\Barcode\BarcodeServiceProvider::class,
],
'aliases' => [
....
'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
]
Create New Controller
Furthermore, you need a sole controller file to add the code for showing multiple barcodes, execute the following command to generate a controller.
php artisan make:controller HomeController
Update resources/views/HomeController.blade.php file.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
return view('index');
}
}
Register Route
The controller’s code doesn’t work by itself; rather, it wants a route on its behalf to execute the controller.
Get into the routes/web.php and create a new route by adding the following code.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index'])->name('create.barcode');
Implement Barcode Generator in View
Now, you have to conjugate the comprehensive code and it will show the various types of barcodes, go to the resources/views folder, create a new index.blade.php file.
Insert the provided code in the resources/views/index.blade.php file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Generate Barcode in Laravel</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
<div class="barcodeCode">
<div>{!! DNS2D::getBarcodeHTML('4445645656', 'QRCODE') !!}</div>
<div>{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA') !!}</div>
<div>{!! DNS1D::getBarcodeHTML('4445645656', 'KIX') !!}</div>
<div>{!! DNS1D::getBarcodeHTML('4445645656', 'UPCA') !!}</div>
<div>{!! DNS1D::getBarcodeHTML('4445645656', 'RMS4CC') !!}</div>
<div>{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA2T') !!}</div>
<div>{!! DNS1D::getBarcodeHTML('4445645656', 'CODABAR') !!}</div>
</div>
</body>
</html>
Bar-code generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extension, 5-Digits UPC-Based Extension, EAN 8, EAN 13, UPC-A, UPC-E, MSI.
Head over to official Git repo to customize the barcodes in laravel further.
Run App in Browser
So, far we have described everything relentlessly, and we are now left with testing the app.
Hence, you have to start the app and view the app in the browser using the provided url.
php artisan serve
http://127.0.0.1:8000
Summary
In this eloquent guide, you have learned how to preciously create barcodes in the laravel app using the barcode generator library.
Of course, this was just the basic tutorial for the novice developers; however, we have tried to highlight the most significant aspect of creating this much-needed feature.
This tutorial ends here, and we hope you have known how to generate QR codes in the PHP Laravel app.