A barcode or bar code is a method of representing data in a visual, machine-readable form. A barcode is a square or rectangular image consisting of a series of parallel black lines and white spaces of varying widths that can be read by a optical barcode scanner.
Now, lets go through step by step and see how we can generate Barcode in Laravel 8.
Open terminal and install fresh laravel application using below command:
composer create-project --prefer-dist laravel/laravel barcodeapp
Next, lets get inside the barcodeapp using below command:
cd barcodeapp
Next, we need to install milon/barcode package for barcode generation. Type below command in terminal:
composer require milon/barcode "^8.0"
Next, open config/app.php file and add service provider and aliase.
<?php
return [
'providers' => [
....
Milon\Barcode\BarcodeServiceProvider::class,
],
'aliases' => [
....
'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
]
];
Next, we will create a route to test our example code. Open routes/web.php file and add below route in it:
<?php
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@'index');
Next, create BarCodeController.php file and put below code in it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BarCodeController extends Controller
{
public function index()
{
return view('barcode');
}
}
Next, create barcode.blade.php file and put below code in it:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Generate Barcode in Laravel 8</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container mt-3">
<div class="row">
<div class="col-12 col-sm-6 col-md-4 mb-3">
<div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'PHARMA') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'CODABAR') !!}</div>
<div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'UPCA') !!}</div>
</div>
</div>
</div>
</body>
</html>
php artisan serve
Next, open below url in browser and see the result:
http://127.0.0.1:8000/barcode
HAPPY LEARNING :)