How to Quickly Generate Barcode in Laravel 9 Application

The step-by step tutorial to Generate Barcode in Laravel will help you efficiently generate a barcode using the milon barcode generator package.

Barcode is a visually represents data in a script form that has bars and spaces having different widths and spacings between parallel lines. Machines decipher this barcode and display it in a human-readable form.

We can easily integrate a barcode generating feature in Laravel. Using this tutorial, we will reveal how you can use a third-party package barcode package and generate various barcodes with basic text and numerical values.

The Barcode Generator is simple and awesome package, which offers many options to customize barcodes in Laravel. Moreover, it also allows you to generate image barcode in Laravel. We can implement the generate barcode in Laravel with few simple steps. Let’s go through the tutorial to learn how to use a third-party package for this process.

Main Steps to Generate Barcode in Laravel 9

Step 1: Create Laravel Project

Open command line interface, type the command below and press enter to start installing a new Laravel app.

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

Navigate to the newly created project’s directory.

cd laravel-barcode

Step 2: Add Database Details

We will now create a database connection, and add database credentials in the .env file. This step is required only if your application interacts with a database.


If you are using macOS or local MAMP server, you also need to append UNIX_SOCKET and DB_SOCKET database credentials in your .env file, as shown below.


Step 3: Install the Barcode Package

Next, add the command as shown below to install the Barcode library.

composer require milon/barcode

Step 4: Register Barcode Library

We will now get in the config/app.php file and add the barcode service provider to register the Barcode package in the provider’s array of your Laravel app. You may also add alias to the values, as shown below.

    return [
    'providers' => [
    'aliases' => [
        'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
        'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,

Step 5: Set Up New Controller Class

Next, you can generate a new controller; that we will use it to load the blade view file.

php artisan make:controller ProductController

Open your resources/views/ProductController.blade.php file and update it with the code shown below.

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

Step 6: Add New Barcode Route

We will now learn how to define a new route using the get method. This will interact with the controller and provide a link to view the app in a browser. So update your routes/web.php file with the code below.

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 Logic in View

Lastly, you will create a view blade file in the views folder, and define their the code below.

Examples of some barcode generators as follows. You can use these to generate many different barcodes.

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

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Laravel Generate Barcode Examples</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="" rel="stylesheet"/>
    <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>        

Step 6: Start and Run Application

You can now start your Laravel development server.

php artisan serve

Use the URL given below or your localhost URL to test the app.
Create Barcode in Laravel


To sum up, this guide shows the use of barcode library to create a barcode generation module in your Laravel app. Moreover, it also allows to easily set the height, width and color of a barcode image in Laravel. You may also create 1D and 2D barcodes using this library.

Leave a Comment