Laravel 9 Barcode Generator Tutorial Example

Hi Dev,

In This Article, i will apportion you how to engender barcode utilizing laravel 8. so you can just follow my step and engender barcode utilizing milon/barcode package let's implement it.

In this Tutorial i would relish to show you how to engender barcode in laravel 8 milon/barcode package.

In this blog, i will utilize milon/barcode package to engender simple text, numeric and image barcode in laravel 8 app.

So, let's start the example and follow my all steps.

Step 1 : Install Laravel 8 Application

In 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 blog
Step 2 : Database Configuration

In this second step, we are require configure database with your downloded/installed laravel 8 app. So, you require to find .env file and setup database details as following:

Path : .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password
Step 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/barcode
Step 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 engender a two route index() method define a simple barcode engender with getBarcodeHTML method and second routes define a image barcode engenderer utilizing getBarcodePNG. in web.php file which located inside routes directory:

Path : routes/web.php
<?php   
use App\Http\Controllers\BarcodeController;
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::class,'index'])->name('barcode');
Route::get('imgbarcode',[BarcodeController::class,'imgbarcode'])->name('imgbarcode');
Step 6 : Creating BarcodeController

Now this step,I will engender generate BarcodeController file by using the following command.

php artisan make:controller BarcodeController

After,successfully created BarcodeController after app/http/controllers and open BarcodeController.php file. And add the simple barcode generation code into it.

Path : app/Http/Controllers/BarcodeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class BarcodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        return view('barcode');
    }

    /**
     * image generate barcode
     *
     * @return response()
     */
    public function imgbarcode()
    {
        return view('imgbarcode');
    }

}

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 explicate a different barcode engender 1D and 2D dimension utilizing 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 - CodingTracker</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 - CodingTracker</h3>

                
                <div>{!! DNS1D::getBarcodeHTML('0987654321', 'C39') !!}</div><br>
                <div>{!! DNS1D::getBarcodeHTML('7600322437', 'POSTNET') !!}</div></br>

                
                <div>{!! DNS2D::getBarcodeHTML('http://codingtracker.blogspot.com/', 'QRCODE') !!}</div><br><br>
                <div>{!! DNS2D::getBarcodeSVG('http://codingtracker.blogspot.com/', 'DATAMATRIX') !!}</div>
            </div>
        </div>
    </div>
</body>
</html>
Preview

In this second imgbarcode.blade.php file i will define a barcode generate using getBarcodePNG method.

Path : resources/views/imgbarcode.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 - CodingTracker</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 - CodingTracker</h3>

                
                <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('CodingTracker', '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" /><br/><br/>
            </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..

Post a Comment