Dynamic barcode generate in laravel example

  • 23-12-2022
  • 855
  • Laravel 9
  • Haresh Chauhan

Barcode generates integration in the laravel application this tutorial will help you to integrate into your application in a very simple way, I have a written post and each step for your simplicity to integrate barcode. using composer milon/barcode install in the app and setting up a little configuration in the config file. in this post, I have also defined different version installations.

After installing the milon/barcode package we will register the service provider and facade in the laravel configuration file. also, we will generate a barcode configuration file using the command for the customization setting in the barcode.

Using milon/barcode we will quickly generate a barcode in the laravel application, the barcode scanner is especially helpful when you developing an eCommerce website also each product separate unique product code barcode, so the user can identify product information by scanning the code.

So let's start integrating barcodes by taking the help of the below example.

Step 1. Install Composer

The user below command in your project command prompt, this command will install some core dependencies in your project application.

composer require milon/barcode

You can also install using the edit composer.json file to the required "milon/barcode". here below given the version-wise composer guidelines. just paste it into your composer.json file and update your composer.

 "require": {
     "milon/barcode": "^9.0"
 }

use for laravel 8.* version.

 "require": {
     "milon/barcode": "^8.0"
 }

use for laravel 7.* version.

 "require": {
     "milon/barcode": "^7.0"
 }

use for laravel 6.* version.

 "require": {
     "milon/barcode": "^6.0"
 }

After pasting in your composer.json file above provided configuration, now you required to update the composer using provided command in your application command prompt.

composer update

Step 2. Configuration & Setup

Open your config/app.php and go to the "providers" attribute. and register Milon\Barcode\BarcodeServiceProvider::class provider class in the providers attribute.

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

If you are running laravel version 4.* then you need to register the below provided registration service class in the config/app.php file.

 'providers' => array(
     // ...
     'Milon\Barcode\BarcodeServiceProvider',
 )

If you wish to customize the barcode configuration, just publish using provided command and generate. this file will generate in the config folder from the root of the application.

 # Laravel 5.x
 php artisan vendor:publish
 
 # Laravel 4.x
 php artisan config:publish milon/barcode

Add facade in the "aliases" attribute in the config/app.php file.

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

If you are running the 4.2 laravel version then your aliases' facade name will be different then the latest version. so make sure your laravel version is before getting installed and config in the application.

 'aliases' => array(
     // ...
     'DNS1D' => 'Milon\Barcode\Facades\DNS1DFacade',
     'DNS2D' => 'Milon\Barcode\Facades\DNS2DFacade',
 )

Step 3. Use & Generate

Create a DemoController in your application using the below provider command.

php artisan make:controller DemoController

Add a route in your route file. and create a controller for the view blade file.

routes/web.php
<?php
 
 use Illuminate\Support\Facades\Route;
 use App\Http\Controllers\DemoController;
 /*
 |--------------------------------------------------------------------------
 | 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',[DemoController::class,'barcodeGenerate']);
 

Controller for the view blade file of the barcode blade file and compact data.

App\Http\Controllers/DemoController.php
<?php
 
 namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
 
 class DemoController extends Controller
 {
     public function barcodeGenerate(){
 
         $data = [
             'code' => 'SSD4564',
             'link' => 'https://www.webappfix.com'
         ];
 
         return view('barcode',compact('data'));
     }
 }

Some examples of barcode generate I blade file. how to use it in a real application. This is just a demonstration purpose example.

barcode.blade.php
 <!DOCTYPE html>
 <html>
 <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Barcode Generate - Webappfix</title>
 </head>
 <body>
      <div>
           <p>{{ $data->code }}</p>
           <p>{!! DNS1D::getBarcodeHTML($data->link, 'C128'); !!}</p>
           <p>Link : {{$data->link}}</p>
      </div>
 
      <div>
           <p>{{ $data->code }}</p>
           <p>{!! DNS2D::getBarcodeHTML($data->link, 'QRCODE'); !!}</p>
           <p>Link : {{$data->link}}</p>
      </div>
 
      <div>
           <p>{{ $data->code }}</p>
           <p>{!! DNS2D::getBarcodeSVG($data->link, 'DATAMATRIX'); !!}</p>
           <p>Link : {{$data->link}}</p>
      </div>
 </body>
 </html>

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :