How to Generate Barcode in Laravel 10 Example

Using the Laravel 10 barcode generator example tutorial you will learn how to generate barcode in Laravel application using milon/barcode package. In this tutorial, we are going to use milon/barcode package to implement barcode in the Laravel app.

The Barcode Generator package is awesome, simple, and offers tons of options to customize barcodes in Laravel, and you can generate image barcode in Laravel using this package.

#1 Install Laravel App

So, first you need to create a laravel project using the following command.

composer create-project laravel/laravel laravel-app

Next go to the application directory:

cd laravel-app

#2 Configure Barcode Package

Next, Install the barcode package using the following command in your laravel application.

composer require milon/barcode

After the barcode package installation process, now open the config/app.php and add the barcode service provider and aliases to the providers’ and aliases array just like below.

'providers' => [
	Milon\Barcode\BarcodeServiceProvider::class,
],
'aliases' => [
    'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
    'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
],

#3 Create Controller

Now generate a controller using the below command.

php artisan make:controller BarcodeController

Next,

Open app\Http\Controllers\BarcodeController.php and update with the given code.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class BarcodeController extends Controller
{
    public function barcode()
    {
      return view('barcode');
    }
}

#4 Make Route for Generate Barcode

Now, we will define our routes in the routes/web.php file for generating barcodes in Laravel application.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BarcodeController;

Route::get('barcode', [BarcodeController::class, 'barcode']);

#5 Create Blade File

In this section, create the blade file inside the resources/views folder. Now open the resources/views/barcode.blade.php file and update the below code on it.

Here are some of the barcode generators that you can use to generate a variety of barcodes.

Like QR Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extension, 5-Digits UPC-Based Extension, EAN 8, EAN 13, UPC-A, UPC-E, MSI.

<html>
	<head>
		<title>Barcode - Laravel</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	</head>
	<h1 class="text-primary" style="text-align: center;margin-bottom: 20px;">Laravel Barcode Generator Example</h1>
	<div style="text-align: center;">
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('11', 'C39')}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('123456789', 'C39+',1,33,array(0,255,0), true)}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('4', 'C39+',3,33,array(255,0,0))}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('12', 'C39+')}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('23', 'POSTNET')}}" alt="barcode" /><br/><br/>
	</div>
</html>

#6 Start your Application

Now, start your application using the serve command to check the barcode implementation in laravel application.

php artisan serve

Next, see in your browser using the following URL

http://127.0.0.1:8000/barcode

I hope you enjoy how to generate barcodes in Laravel application.