Generate Barcode in Laravel

Last modified on December 8, 2020 1 min read

A barcode or bar code is a method of representing data in a visual, machine-readable form. In this article, we’re going to generate barcode in Laravel. I’m testing on Laravel 8.9.0. Let’s get started:

Table of Contents

  1. Install Laravel and Basic Configurations
  2. Install Package & Configure
  3. Create Controller
  4. Create View File
  5. Define Route
  6. Run & Test

Install Laravel and Basic Configurations

Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.

Install Package & Configure

Install milon/barcode package in the project:

composer require milon/barcode

Laravel 5.5 uses package auto-discovery, so doesn’t require you to manually add the service provider & aliases. If you don’t use auto-discovery, add the service provider & aliases in config/app.php file like:

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

Create Controller

Run this command to create a barcode controller:

php artisan make:controller BarCodeController

Now open the controller and paste this code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BarCodeController extends Controller
    // index
    public function index()
        return view('barcode');

Create View File

Make a blade view file named barcode.blade.php and paste the below code:

<!doctype html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Generate Barcode in Laravel -</title>
<div class="container text-center" style="margin-top: 50px;">
    <h3 class="mb-5">Generate Barcode in Laravel</h3>
    <div>{!! DNS1D::getBarcodeHTML('4445645656', 'C39') !!}</div></br>
    <div>{!! DNS1D::getBarcodeHTML('4445645656', 'POSTNET') !!}</div></br>
    <div>{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA') !!}</div></br>
    <div>{!! DNS2D::getBarcodeHTML('4445645656', 'QRCODE') !!}</div></br>

Define Route

Open routes/web.php and register this route:

use App\Http\Controllers\BarCodeController;

Route::get('/barcode', [BarcodeController::class, 'index']);

Run & Test

Now run the project, visit /barcode route & test.

// run application
php artisan serve

// visit the route


Note: We can set width and height, color, add text, 1 & 2D barcodes etc. using this package. Have a look at milon/barcode GitHub repository to see more examples.

That’s all, folks. Thanks for reading. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.


Leave a Reply

Your email address will not be published. Required fields are marked *