How to Quickly Generate Barcode in Laravel 10 Application

Last Updated on by in Laravel

In this tutorial, you’ll learn how to quickly generate barcodes in a Laravel application using the milon/barcode package.

The milon/barcode package is a Laravel library that provides an easy way to generate a variety of 1D and 2D barcodes, including QR Codes, UPC-A, EAN, and more. Key benefits include:

  • Easy Integration: Seamlessly integrates with Laravel applications.
  • Variety of Barcode Formats: Supports numerous barcode formats.
  • Customizable: Allows customization of barcode appearance.
  • Flexibility: Generates barcodes based on dynamic data.
  • Multiple Output Formats: Outputs barcodes in HTML, PNG, or SVG formats.

Overall, milon/barcode is a versatile package that simplifies the process of adding barcode generation to your Laravel projects.

We’ll walk you through creating a simple webpage that showcases various barcode formats such as QR Code, PHARMA, PHARMA2T, CODABAR, KIX, RMS4CC, and UPCA.

To follow along, you’ll need to have a Laravel application set up and the required barcode packages installed. We’ll explain how to set up the necessary components, create a view file with the provided code snippet, and customize the output as needed.

The code snippet uses DNS2D::getBarcodeHTML for generating a QR Code and different variations of DNS1D::getBarcodeHTML for generating 1D barcodes. By customizing the inputs, you can produce barcodes for various data and formats, which can be displayed on a web page for further use or printing.

This tutorial is perfect for Laravel developers looking to add barcode generation to their applications quickly and efficiently. By the end of the guide, you’ll have a functional understanding of barcode generation using Laravel and the ability to apply it in your projects.

How to Create Barcode in Laravel 10 App

  • 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

Create Laravel Project

Let us start from the beginning, hence 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

Move into the project directory.

cd laravel-demo

Add Database Details

Create the database connection, add database credentials in .env file, this step is required only if you want to interact with the database for data maangement.

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

If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Install Barcode Package

In this section, we will tell you what command to use to install the Barcode package, type and execute the command to begin installing the barcode library.

composer require milon/barcode

Register Barcode Library

After completing the package installation process, get into the config/app.php and add the barcode service provider to the providers’ array. Additionally, add the values into the alias as well.

<?php
    return [
    'providers' => [
        ....
        ....
        ....                
        Milon\Barcode\BarcodeServiceProvider::class,
    ],
    
    'aliases' => [
        ....
        ....
        ....                
        'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
        'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
    ]

Set Up New Controller

Next, use the given command to generate a controller; we will use it to load the blade view file.

php artisan make:controller ProductController

In this section, open resources/views/ProductController.blade.php and update with the given code.

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

Add Barcode Route

In this section, we will show you how to define a new route with the get method; this route interacts with the controller and provides a link to view the app in the browser, thus 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');

Implement Barcode in View

In the last section, you need to create the view file inside the views folder, define the given code in the resources/views/product.blade.php file.

Here are some of the barcode generators that you can use to generate a variety of barcodes.

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

<!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>

Start Application

php artisan serve
http://127.0.0.1:8000/generate-barcode

Create Barcode in Laravel

Conclusion

In conclusion, the provided code allows you to generate various types of barcodes with ease in your Laravel application. Customize the format and data as needed to suit your project’s requirements.

Age calculator tool

🎂✨ Discover your age in detail and with countdown to your next birthday with fun facts! 🎈🎉

Calculate your age now!
Digamber - Author positronX.io

Hi, I'm Digamber Singh, a New Delhi-based full-stack developer, tech author, and open-source contributor with 10+ years' experience in HTML, CSS, JavaScript, PHP, and WordPress.