Generate Barcode In Laravel Application Using Milon/barcode

You can quickly generate barcodes in the Laravel project with the milon/barcode generator package. In this article, we will show you how to do it step by step.


A barcode is a way to show data visually in a scripted way. It is made up of parallel lines of different widths and distances from each other. Machines can read barcodes because they are written in a way that machines can understand.


No longer is it hard to add a barcode-generating feature to Laravel. This guide will show you how to use a third-party barcode package to make different barcodes with simple text and numbers.


The Barcode Generator package is excellent and easy to use. It gives you a lot of ways to customize barcodes in Laravel. You can also use this package to generate image barcodes in Laravel.

Step 1: Create a Laravel Application

Let's get things rolling by opening the console, using the provided command, and pressing enter to have the composer set up the new Laravel application.


composer create-project --prefer-dist laravel/laravel laravel-blog


Enter the working folder of the project.


cd laravel-blog

Step 2: Add Database

Suppose you wish to manage data by interacting with the database. In that case, you must first set up a connection and include your database credentials in the .env file.


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_user_name
DB_PASSWORD=db_password


Step 3: Install the milon/barcode Package.

Here, you'll find the command to set up the Barcode package; after you've copied and pasted the command, run it to start setting up the barcode library.


composer require milon/barcode


Step 4: Register for Barcode Library

After installing the package, go to the config/app.php file and add the barcode service provider to the array of providers. Also, add the barcode 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 Controller

Next, construct a controller by executing the following command; later, we will use this controller to load the blade view file.


php artisan make:controller ProductController


Open the app/Http/Controllers/ProductController.php file, and then change it using the code that has been provided.


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
    public function index()
    {
      return view('product');
    }
}

Step 6: Add Route

This part will demonstrate how to create a new route using the get method. This route communicates with the controller and gives a link to open the app in a web browser and update 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: Barcode View Implementation

You must build the view file within the views folder and put the provided code in the resources/views/product.blade.php file in the last section.


You may generate various barcodes with the help of the following barcode generators listed below for your convenience.


Similar to QR Code, PDF417, C128A, C128B, C128C, S25, S25+, I25, I25+, C93, C128, C128A, C128E, and C39, EAN 8, EAN 13, UPC-A, UPC-E, 2-Digits UPC-Based Extension, 5-Digits UPC-Based Extension, and MSI.


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Generate Barcode in Laravel Application</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>


Run the application with given command


php artisan serve


Visit the below url in your browser. It will show the generated barcodes.


http://127.0.0.1:8000/generate-barcode