How to Quickly Generate Barcode in Laravel 8 Application

Last updated on: by Digamber

Laravel 8 barcode generator tutorial; This step-by-step guide will help you find out how to efficiently generate Barcode in the Laravel app using the milon barcode generator package from scratch.

A barcode is a visual representation of data in a scripted form that contains bars and spaces with various widths and spacings of parallel lines. Machines decipher barcode because it is displayed in machine-readable form.

Implementing the barcode generating feature in laravel is no more difficult; through 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 package is awesome, simple, and offers tons of options to customize barcode in laravel, and you can generate image barcode in laravel using this package.

How to Create Barcode in Laravel 8 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

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

We have explained how to comfortably create a barcode generation module in laravel using the barcode generate package. Also, It is easy to set the width, height, color of the barcode image in laravel; moreover it can create 2D barcodes and 1D barcodes with it.

Digamber

I am Digamber, a full-stack developer and fitness enthusiast. I crafted this site to bestow my coding experience and love to write on JavaScript, React, Angular, Vue, Laravel.