Hello buddies, How are you? In this post I am going to show you how to generate barcode in laravel 8. I’d want to demonstrate how to create a barcode in Laravel 8. This post will show you how to store a created bar code in Laravel using a basic example. This example will aid you in understanding the Laravel 8 barcode tutorial.
Preview:
Create Laravel project
composer create-project --prefer-dist laravel/laravel blog
Set Database Configuration properties
Go to the .env file and set you Database name, Database username and password.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelblog
DB_USERNAME=root
DB_PASSWORD=
Install picqer/php-barcode-generator Package
We need to install picqer/php-barcode-generator package for barcode generate, this is the simplest way to do that. So Open your terminal and run bellow command.
composer require picqer/php-barcode-generator
Create Route
Lets create a route for testing example. So, let’s add new route on that file.
routes/web.php
Route::view('barcode-generator', 'barcode-view');
Create Blade file
It’s time to create barcode-view.blade.php for display bar code. so let’s create blade file as like bellow code:
resources/views/barcode-view.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Generate Bar Code in Laravel 8.0</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1>How to Generate Bar Code in Laravel 8.0</h1>
<div class="row mt-5">
<div class="col-md-6 ">
<div class="border border-primary p-3">
<h3>Product 1: 0890823232435</h3>
@php
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
@endphp
{!! $generator->getBarcode('0890823232435', $generator::TYPE_CODE_128) !!}
</div>
</div>
<div class="col-md-6">
<div class="border border-primary p-3">
<h3>Product 2: 1212089787975</h3>
@php
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
@endphp
<img src="data:image/png;base64,{{ base64_encode($generatorPNG->getBarcode('1212089787975', $generatorPNG::TYPE_CODE_128)) }}">
</div>
</div>
</div>
<div class="row mt-5">
<div class="col-md-6 ">
<div class="border border-primary p-3">
<h3>Product 3: 21786708866</h3>
@php
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
@endphp
{!! $generator->getBarcode('21786708866', $generator::TYPE_CODE_128) !!}
</div>
</div>
<div class="col-md-6">
<div class="border border-primary p-3">
<h3>Product 4: 3238767564</h3>
@php
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
@endphp
<img src="data:image/png;base64,{{ base64_encode($generatorPNG->getBarcode('3238767564', $generatorPNG::TYPE_CODE_128)) }}">
</div>
</div>
</div>
<div class="row mt-5">
<div class="col-md-6 ">
<div class="border border-primary p-3">
<h3>Product 5: 676734398975</h3>
@php
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
@endphp
{!! $generator->getBarcode('676734398975', $generator::TYPE_CODE_128) !!}
</div>
</div>
<div class="col-md-6">
<div class="border border-primary p-3">
<h3>Product 6: 8764565676343</h3>
@php
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
@endphp
<img src="data:image/png;base64,{{ base64_encode($generatorPNG->getBarcode('8764565676343', $generatorPNG::TYPE_CODE_128)) }}">
</div>
</div>
</div>
</div>
</body>
</html>
Run the project
php artisan serve
Go to : http://127.0.0.1:8000/barcode-generator
Output:
Hope it helps!