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 visual representation of data in a script form that has bars and spaces with different widths and spacings between parallel lines. Machines can decipher this barcode as it is displayed in a machine-readable form.

Implementing the barcode generating feature in Laravel is no more difficult. 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

Main Steps to Generate Barcode in Laravel 9

Step 8: Start Application

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

Secondly, we will create a database connection, you may add database credentials in .env file, the step is required only if your application has to interact with database.


In case you are using macOS or local MAMP server, you will also need to append UNIX_SOCKET and DB_SOCKET database credentials in your .env file.


Step 3: Install the Barcode Package

Now type in the command as shown below to install the Barcode library.

composer require milon/barcode

Step 4: Register Barcode Library

Next, you need to get in the config/app.php file and add the barcode service provider to register your 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 resources/views/ProductController.blade.php file and update it with the code 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 you how to define a new route using the get method. It will interact with the controller and provides a link to view the app in the browser. So update the routes/web.php file as shown 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 a variety of 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 the Laravel development server.

php artisan serve

And use the URL below or your localhost URL to test the app.
Create Barcode in Laravel


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

Leave a Comment