How to generate Barcode in Laravel 8

How to generate Barcode in Laravel 8

Last Updated: 30 Nov, 2022

What is Barcode?


A barcode or bar code is a method of representing data in a visual, machine-readable form. A barcode is a square or rectangular image consisting of a series of parallel black lines and white spaces of varying widths that can be read by a optical barcode scanner.

Some realtime examples of barcode usage:
  • Barcodes are applied to products for quick identification.
  • Barcodes are used in retail stores as part of the purchase process.
  • Barcodes are used in warehouses to track inventory.
  • Barcodes are used on invoices to assist in accounting.

Now, lets go through step by step and see how we can generate Barcode in Laravel 8.

Step 1: Create fresh laravel 8 application

Open terminal and install fresh laravel application using below command:

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

Next, lets get inside the barcodeapp using below command:

cd barcodeapp

Step 2: Install Barcode laravel package

Next, we need to install milon/barcode package for barcode generation. Type below command in terminal:

composer require milon/barcode "^8.0"

Next, open config/app.php file and add service provider and aliase.

<?php
return [
    'providers' => [
        ....                
        Milon\Barcode\BarcodeServiceProvider::class,
    ],

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

Step 3: Create route

Next, we will create a route to test our example code. Open routes/web.php file and add below route in it:

<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/barcode', 'BarCodeController@'index');

Step 4: Create controller

Next, create BarCodeController.php file and put below code in it:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

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

Step 5: Create balde file

Next, create barcode.blade.php file and put below code in it:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Generate Barcode in Laravel 8</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
    <div class="container mt-3">
        <div class="row">
            <div class="col-12 col-sm-6 col-md-4 mb-3">
                <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'PHARMA') !!}</div>
                <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'CODABAR') !!}</div>
                <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'UPCA') !!}</div>
            </div>
        </div>
    </div>
</body>
</html>

Step 6: Start the development server using below command:

php artisan serve

Next, open below url in browser and see the result:

http://127.0.0.1:8000/barcode

HAPPY LEARNING :)

 

Thank You, Please Share.