Laravel

How to Generate Barcode in Laravel 10?

Hello Laravel friends,

In today’s Laravel tutorial, I will provide a step-by-step process to Generate a Barcode in Laravel 10.

Barcodes are ubiquitous in the world of business and inventory management. They simplify the process of tracking products, assets, and various other items. Let’s explore how you can generate a barcode for your Laravel 10 application.

Steps to Generate Barcode in Laravel 10:

Step 1: Install Laravel using the below command.

composer create-project laravel/laravel laravel-blog

Step 2: Install composer for barcode using the below command.

composer require picqer/php-barcode-generator

Step 3: Create a controller and create a function in the controller. For that, add the below code in the web.php file.

Route::get('/barcode-index', [BarcodeController::class, 'barcodeIndex']);

Now, add the below code to your controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BarcodeController extends Controller
{
    public function barcodeIndex(Request $request)
    {
        $generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
        $image = $generator->getBarcode('081331723987', $generator::TYPE_CODE_128);
  
        return response($image)->header('Content-type','image/png');
    }
}

Output: 

Step 4: You can also save your barcode image in storage using the below code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class BarcodeController extends Controller
{
    public function barcodeIndex(Request $request)
    {
        $generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
        $image = $generator->getBarcode('081331723987', $generator::TYPE_CODE_128);

        Storage::put('barcodes/barcode.png', $image);

        return response($image)->header('Content-type','image/png');
    }
}

Output:

Step 5: You can also show your barcode in the blade file. For that, add the code below to your controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class BarcodeController extends Controller
{
    public function barcodeIndex(Request $request)
    {
        $generator = new \Picqer\Barcode\BarcodeGeneratorHTML();
        $barcode = $generator->getBarcode('0001245786925', $generator::TYPE_CODE_128);
  
        return view('barcode', compact('barcode'));
    }
}

Now, add the below code in the blade file.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Barcode Generate</title>
    </head>
    <body>
        <h3>Your Barcode</h3>
        {!! $barcode !!}
    </body>
</html>

Output:

Conclusion:

With this process, you can integrate barcode generation into your Laravel applications, enhancing your inventory management or any other barcode-related functionalities. Share the tutorial with your friends to help them generate barcodes for their Laravel application. If you come across any difficulty, leave a comment or get in touch with Laravel developers.

Happy Coding!

Click to rate this post!
[Total: 6 Average: 4]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

View Comments

  • Clear and concise guide on generating barcodes in Laravel 10! The step-by-step instructions provided in this article make it easy for developers to implement barcode generation seamlessly. Thanks for sharing this valuable resource!

Recent Posts

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

1 day ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

3 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

4 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

4 days ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

4 days ago

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

5 days ago