Laravel 12 Barcode Generate using Milon Barcode

Laravel 12 can create barcode image for different types of project easily. The popular package for barcode generation is milon/barcode. Tn this article we will use this package for making barcode image. 

Here we will follow some steps to make barcode

  1. Install or Create Laravel 12 Project
  2. Install Milon/Barcode Composer Package
  3. Create Necessary Route
  4. Create Necessary Controller
  5. Create Blade File
  6. See Output
Install or Create Laravel 12 Project

We are now creating a empty laravel 12 project. You can also use your existing laravel project as well.

composer create-project --prefer-dist laravel/laravel laravel12-barcode
cd laravel12-barcode

Install Milon/Barcode Composer Package

We have successfully created our project. Now we will use milon/barcode composer package. This package is so much popular among laravel developers around the world. Let's install the package from command line and publish the configuration

composer require milon/barcode

Define Necessary Route

We will create a route from web.php file. This route will accept request and return blade output in browser.

use App\Http\Controllers\BarcodeController;
use App\Models\User;
use Illuminate\Support\Facades\Route;

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

Create Necessary Controller

As route is sending action to view method of BarcodeController. So we need to create a controller

php artisan make:controller BarcodeController

BarcodeController

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BarcodeController extends Controller
{
    /**
     * Display the user's profile form.
     */
    public function view(Request $request)
    {
        return view('barcode');
    }
}

Create Blade File

To see output and render barcode we need to have a blade file. Let's create a blade file called barcode.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Barcode</title>
    <style>
        body{
            margin: 100px auto;
            display: flex;
            justify-content: center;
        }
    </style>
</head>
<body>
    {!!  '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG('456', 'C39+') . '" alt="barcode"   />';!!}
</body>
</html>

Read also: Laravel 11 chat app using reverb broadcasting event

Read also: Laravel 12 two factor authentication 2fa with otp login verification

Run the Project
After running the project and visiting route /view we will the below output in browser like below. If we scan using any mobile app or barcode scanner we will see the text 
If you think why barcode is necessary for application then you can see below points as reference.
  1. Scanning a barcode is much quicker and less error-free than manually typing numbers or names.
  2. Businesses use barcodes to track stock levels, manage shipments data, and avoid over/under-stocking.
  3. Barcodes allow for quick product lookup while checkout, enabling faster transactions capability.
  4. Companies use barcodes to track assets like equipment, tools, or office supplies for internal management.
  5. Packages are barcoded to track their journey from source to destination, enabling real-time updates.
  6. Hospitals use barcodes for patient wristbands, medications, and equipment to prevent mix-ups and ensure safety.
  7. Barcodes on files or documents make archiving, indexing, and retrieval easier and more efficient.
  8. Barcodes (especially QR codes) are used in mobile payments, ticketing, website links, and authentication.

Read also: Laravel 12 breeze authentication login registration

If you find this article helpful, then follow our website as well as youtube channel. Give us your valuable feedback if you have.

Tags: