Laravel 8 Barcode Generator Example Tutorial
Jun 02, 2021 . Admin
Hi Friends,
Today, In this article i will share you how to generate barcode using laravel 8. so you can just follow my step and generate barcode using milon/barcode package let's implement it.
In this Tutorial i would like to show you how to generate barcode in laravel 8 milon/barcode package.
In this blog, i will use milon/barcode package to generate simple text, numeric and image barcode
So, let's start the example and follow my all steps.
Step 1 : Install Laravel 8 ApplicationIn the first step first of all we are learning from scratch, So we need to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blogStep 2 : Database Configuration
In this second step, we are require configure database with your downloded/installed laravel 8 app. So, you need to find .env file and setup database details as following:
Path : .envDB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db name DB_USERNAME=db user name DB_PASSWORD=db passwordStep 3 : Installing milon/barcode Package
Now,In this third step we will install laravel milon/barcode package via following command. so open your command prompt and paste the bellow code.
composer require milon/barcodeStep 4 : Configure Barcode Generator Package
Here, In this step,we will configure the milon/barcode package in laravel 8 app. So, Open the providers/config/app.php file and register the provider and aliases for milon/barcode and paste the bellow code.
'providers' => [ .... Milon\Barcode\BarcodeServiceProvider::class, ], 'aliases' => [ .... 'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class, 'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class, ]Step 5 : Create Route
Here, in this step we will create a two route index() method define a simple barcode generate with getBarcodeHTML method and second routes define a image barcode generator using getBarcodePNG. in web.php file which located inside routes directory:
Path : routes/web.php<?php use App\Http\Controllers\BarcodeGeneratorController; 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',[BarcodeGeneratorController::class,'index'])->name('barcode'); Route::get('barcode-img',[BarcodeGeneratorController::class,'imgbarcode'])->name('barcode-img');Step 6 : Creating BarcodeGeneratorController
Now this step,I will engender generate BarcodeGeneratorController file by using the following command.
php artisan make:controller BarcodeGeneratorController
After,successfully created BarcodeGeneratorController after app/http/controllers and open BarcodeGeneratorController.php file. And add the simple barcode generation code into it.
Path : app/Http/Controllers/BarcodeGeneratorController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class BarcodeGeneratorController extends Controller { /** * Write code on Method * * @return response() */ public function index() { return view('barcode'); } /** * image generate barcode * * @return response() */ public function imgbarcode() { return view('img-barcode'); } }Step 7 : Create a Blade File
In this step we will make a two different blade file first is barcode.blade.php file in this blade file i explain a different barcode generate 1D and 2D dimension using getBarcodeHTML method.
Path : resources/views/barcode.blade.php<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Laravel 8 Barcode Generator Example - MyWebTuts.com</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container container justify-content-center d-flex"> <div class="row"> <div class="col-md-12"> <h3 class="">Laravel 8 Barcode Generator Example - MyWebTuts.com</h3> <div>{!! DNS1D::getBarcodeHTML('0987654321', 'C39') !!}</div><br> <div>{!! DNS1D::getBarcodeHTML('7600322437', 'POSTNET') !!}</div></br> <div>{!! DNS1D::getBarcodeHTML('8780395141', 'PHARMA') !!}</div></br> <div>{!! DNS2D::getBarcodeHTML('https://www.mywebtuts.com/', 'QRCODE') !!}</div><br><br> <div>{!! DNS2D::getBarcodeSVG('https://www.mywebtuts.com/', 'DATAMATRIX') !!}</div> </div> </div> </div> </body> </html>Preview
In this second img-barcode.blade.php file i will define a barcode generate using getBarcodePNG method.
Path : resources/views/img-barcode.blade.php<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Laravel 8 Barcode Generator Example - MyWebTuts.com</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container text-center mt-5"> <div class="row"> <div class="col-md-12"> <h3 class="mb-4">Laravel 8 Barcode Generator Example - MyWebTuts.com</h3> <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('MyWebTuts.com', 'C39+',1,33,array(0,0,255), true)}}" alt="barcode" /><br/><br/> <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('1234567890', 'C39+',3,33,array(58, 247, 44), true)}}" alt="barcode" /><br/><br/> <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('Welcome To Our Website', 'C39+',1,33,array(255, 0, 0), true)}}" alt="barcode" /> </div> </div> </div> </body> </html>Preview
Now we are ready to run our bar code laravel 8 example so run bellow command for quick run:
php artisan serve
Now you can open bellow URL on your browser:
Browser url run : http://localhost:8000/barcode
It Will Help You..