Laravel 9 barcode generator tutorial; This step-by-step tutorial will show you how to create a barcode in your Laravel app from scratch using the milon barcode generator package.
A barcode is a programmed visual representation of data that consists of bars and spaces with different lengths and spacings of parallel lines. Barcodes are deciphered by machines because they are shown in a machine-readable format.
It’s no longer difficult to implement the barcode generating feature in Laravel; in this tutorial, we’ll show you how to use a third-party barcode package to generate multiple barcodes with basic text and numerical values.
The Barcode Generator package is fantastic, easy to use, and provides a plethora of options for customising barcodes in Laravel. You can also use this package to produce picture barcodes in Laravel.
How to Create Barcode in Laravel 9 App
Table of Content
- Step 1: Create Laravel Project
- Step 2: Add Database Details
- Step 3: Install Barcode Package
- Step 4: Register Barcode Library
- Step 5: Set Up New Controller
- Step 6: Add Barcode Route
- Step 7: Implement Barcode in View
- Step 8: Start Application
Step 1 – Create Laravel Project
To begin, open the console, type the given command, hit enter, and let the composer tool install the new laravel app for you.
composer create-project --prefer-dist laravel/laravel laravel-demo
Go to the project directory and open it.
cd laravel-demo
Step 2 – Add Database Details
Create a database connection and add database credentials to the.env
file; this step is only required if you wish to interface with the database to manage data.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password
Step 3 – Install Barcode Package
We’ll show you how to install the Barcode package in this part. Type and execute the command to begin installing the barcode library.
composer require milon/barcode
Step 4 – Register Barcode Library
After you’ve finished installing the package, go to config/app.php
and add the barcode service provider to the array of providers. Additionally, provide the values in the alias.
<?php
return [
'providers' => [
....
....
....
Milon\Barcode\BarcodeServiceProvider::class,
],
'aliases' => [
....
....
....
'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
]
Step 5 – Set Up New Controller
Next, construct a controller with the supplied command; we’ll use it to load the blade view file.
php artisan make:controller ProductController
Open resources/views/ProductController.blade.php
in this section and replace the supplied code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
return view('product');
}
}
Step 6 – Add Barcode Route
We’ll teach you how to use the get function to create a new route that interacts with the controller and gives a link to view the app in the browser, updating the routes/web.php file
.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('/generate-barcode', [ProductController::class, 'index'])->name('generate.barcode');
Step 7 – Implement Barcode in View
Finally, in the resources/views/product.blade.php file, define the given code in the resources/views/product.blade.php file.
Here are some barcode generators that you may use to create different types of barcodes.
QR Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, C128, C128A, C128B, C128C, C128, C128A, C128B, C128C, C128, C128A, C128B, C128C, C EAN 8, EAN 13, UPC-A, UPC-E, MSI, 2-Digits UPC-Based Extension, 5-Digits UPC-Based Extension
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel Generate Barcode Examples</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="mb-3">{!! DNS2D::getBarcodeHTML('4445645656', 'QRCODE') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA2T') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'CODABAR') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'KIX') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'RMS4CC') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'UPCA') !!}</div>
</div>
</body>
</html>
Step 8 – Start Application
php artisan serve
http://127.0.0.1:8000/generate-barcode
Conclusion
Using the barcode generate package, we’ve shown you how to easily develop a barcode generator module in Laravel.
In addition, it is simple to set the width, height, and colour of the barcode picture in Laravel; it can also build 2D and 1D barcodes.
I hope you will like the content and it will help you to learn How to Create a Barcode in a Laravel 9 Application
If you like this content, do share.