Laravel 9 Generate and Save Barcode Example Tutorial

Hello Artisan,

In this tutorial, I will show you how to generate and save barcode in laravel 9 application. From this laravel barcode tutorial, i will show you step by step how to create barcode image or html barcode and save it into database using picqer/php-barcode-generator composer package.

We can use another good package for generating barcode like milon/barcode but i am going to use this  picqer/php-barcode-generator package. So this example is for you if you don't know how to generate barcode in Laravel 9 application.

This package does not support any 2D barcodes, like QR codes. But this package is pretty good for generating barcodes in PHP-based applications like Laravel. Using this PHP barcode generator with text package you can generate multitype barcode-like png, svg, gpg or html text also.

In this barcode generator laravel 9 tutorial i will show you step by step and also show you how you can store it in the database and how to print that barcode.



Step 1 : Install picqer/php-barcode-generator Package

In this step, we have to install picqer/php-barcode-generator package to create php barcode generator laravel 9. Run the below command to install it.

composer require picqer/php-barcode-generator


Step 2 : Save Barcode

In this step, we need to save barcode into a database for our product. In this step, we will generate a barcode for our product and then we will save this barcode image into the database. 

Now to generate and save barcode in your php laravel application, follow the below code:

$product = new Product;

$number = $request->sku;
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
$barcode = '< img src="data:image/png;base64,' . base64_encode($generator->getBarcode($number, $generator::TYPE_CODE_128)) . '" >';

$product->barcode = $barcode;


Recommended : Avoid Mass Assignment to Set Unguard Globally For Every Model In Laravel


In the above code, i have used png formatted barcode, but you can use HTML for your barcode, like

$product = new Product;

$number = $request->sku;
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
$barcode = $generator->getBarcode($number, $generator::TYPE_CODE_128);

$product->barcode = $barcode;


Hope it can help you to generate and save barcodes for your product in your php laravel application.


Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js