How to Generate Barcode using Laravel 10 with example

Spread the love

Hello Guys,

Discover How to Generate Barcodes in Laravel 10: Step-by-Step Guide

Learn the process of generate barcode in Laravel 10 with this concise tutorial. This article provides a comprehensive demonstration on creating barcodes within Laravel 10, complete with a simple and practical example. We’ll also delve into the crucial details of preserving your created barcode in Laravel.

In this guide, we will leverage the composer package ‘picqer/php-barcode-generator‘ to effortlessly create barcodes. Our example will cover the creation of barcodes using both TYPE CODE 128 and TYPE CODE 39. Explore the world of barcode generation in Laravel 10 with ease.

Please follow the steps below to Generate Barcode using Laravel 10 projects.

Step 1: Install Laravel 10

While it’s not mandatory, you have the option to execute the following command if you haven’t already established your Laravel application:

Also Read: How to INTEGRATION Razorpay Payment Gateway in Laravel 10

composer create-project laravel/laravel example-app

Step 2: Install picqer/php-barcode-generator

To initiate the installation of the picqer/php-barcode-generator package for generating barcodes within Laravel applications, you should start by opening your terminal and executing the subsequent command:

composer require picqer/php-barcode-generator

Example 1: Laravel Generate Barcode

In this guide, we will outline a simple method for generating barcodes. Additionally, we’ll provide a demonstration of the outcomes.

Also Read: How to Run Specific Seeder in Laravel?

routes/web.php
<?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', function () {
  
        $generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
        $image = $generatorPNG->getBarcode('000005263635', $generatorPNG::TYPE_CODE_128);
  
        return response($image)->header('Content-type','image/png');
 });
Output:
image 19 How to Generate Barcode using Laravel 10 with example

Example 2: Laravel Generate Barcode and Save

In this guide, we’ll outline a simple method for creating barcodes:

Also Read: How to Check Laravel App Environment?

routes/web.php
<?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-save', function () {
  
        $generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
        $image = $generatorPNG->getBarcode('000005263635', $generatorPNG::TYPE_CODE_128);
  
        Storage::put('barcodes/demo.png', $image);
  
        return response($image)->header('Content-type','image/png');
 });

Example 3: Laravel Generate Barcode with Blade

In this guide, we’ll outline a simple method for generating barcodes, and you can view the outcomes demonstrated further down.

routes/web.php
<?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-blade', function () {
  
        $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML();
        $barcode = $generatorHTML->getBarcode('0001245259636', $generatorHTML::TYPE_CODE_128);
  
        return view('barcode', compact('barcode'));
 });
resources/views/barcode.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>How to Generate Barcode using  Laravel 10 with example</title>
</head>
<body>
             
    <h3>Product: 0001245259636</h3>  
    {!! $barcode !!}
  
</body>
</html>
Output:

Also Read: how to use Cron Job Task Scheduling using LARAVEL 10

image 20 How to Generate Barcode using Laravel 10 with example

Discover the best in Laravel blog through our thoughtfully crafted content.

Leave a Comment