How to Generate Barcode in Laravel 9 Example Tutorial

Laravel 9 barcode generator example; In this tutorial you will learn how to generate barcode in laravel 9 using milon/barcode package. Here we will use milon/barcode package and we will implement it in our laravel barcode example.

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.

Step 1: Install Barcode Package

First you need to Install the barcode package using the following command in laravel application.

composer require milon/barcode

Step 2: Add Service Provider

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.

'providers' => [
	
	Milon\Barcode\BarcodeServiceProvider::class,
],

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

Step 3 : Create Controller and Update Code

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

php artisan make:controller BarcodeController

In this section, open app\Http\Controllers\BarcodeController.php and update with the given code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

Step 4: Add 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\BarcodeController;

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

Step 5: Create Blade File 

In the last section, you need to create the view file inside the views folder, define the given code in the resources/views/barcode.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.

<html>
	<head>
		<title>Barcode - Laravel</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	</head>
	<h1 class="text-primary" style="text-align: center;margin-bottom: 20px;">Laravel Barcode Generator Example</h1>
	<div style="text-align: center;">
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('11', 'C39')}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('123456789', 'C39+',1,33,array(0,255,0), true)}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('4', 'C39+',3,33,array(255,0,0))}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('12', 'C39+')}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('23', 'POSTNET')}}" alt="barcode" /><br/><br/>
	</div>
</html>

For more information about barcode package : milon/barcode

So, laravel barcode generate example code implemented you can use in your laravel application and get output like below screen print.

Leave a Comment