Generate And Save Barcode In Your PHP Laravel Application

Hello devs in this brand new example tutorial i am going to show you how to generate and save barcode in you php laravel application. There are many packages for generating barcode in php laravel. 

In this laravel barcode generator example tutorial i won't use milon/barcode package rather i will use /php-barcode-generator package. This package do not support any 2D barcodes, like QR codes. But this package is pretty good for generate barcode. 

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 8 tutorial i will show you step by step and also show you how you can store it into database and how to print that barcode.


Laravel 8 barcode generator example



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. Run below command to install it.

composer require picqer/php-barcode-generator


Step 2 : Save Barcode

In this step we need to save barcode into database for our product. In this step we will generate barcode for our product and then we will save this barcode image into 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 use 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 barcode for your product in your php laravel application.


#laravel #laravel-8x #barcode-generator #picqerphp-barcode-generator