How to Generate Barcode in Laravel 11? | websolutioncode.com
How to Generate Barcode in Laravel 11? | websolutioncode.com

How to Generate Barcode in Laravel 11?

Introduction: Barcodes play a vital role in modern business operations, allowing for efficient tracking and management of products and assets. In Laravel 11, generating barcodes can be accomplished with ease using libraries like “bacon/bacon-qr-code” for QR codes and “milon/barcode” for various barcode formats. This guide aims to provide a step-by-step tutorial on how to generate barcodes in Laravel 11, catering to beginners with straightforward explanations and practical code examples.

Step 1: Setting Up a Laravel Project Before diving into barcode generation, ensure you have a Laravel 11 project set up on your local machine. If not, you can install Laravel using Composer by running the following command in your terminal:

composer create-project laravel/laravel my-project

Navigate to your project directory and start your Laravel development server:

php artisan serve

Step 2: Installing Required Packages To generate barcodes, we’ll need to install the necessary packages via Composer. Open your terminal and run the following commands:

For QR codes:

composer require bacon/bacon-qr-code

For other barcode formats:

composer require milon/barcode

Step 3: Generating Barcodes in Laravel Now, let’s proceed to generate barcodes within our Laravel application. We’ll cover generating both QR codes and other barcode formats.

Generating QR Codes

To generate QR codes, create a new controller or use an existing one. For this example, let’s create a new controller named BarcodeController:

php artisan make:controller BarcodeController

Open the newly created controller file (app/Http/Controllers/BarcodeController.php) and add the following code:

<?php

namespace App\Http\Controllers;

use BaconQrCode\Encoder\QrCode;
use Illuminate\Http\Request;

class BarcodeController extends Controller
{
public function generateQRCode(Request $request)
{
$url = $request->input('url'); // Get URL from request

// Generate QR code
$qrCode = QrCode::encode($url);

// Return QR code image as response
return response($qrCode->get(), 200)
->header('Content-Type', $qrCode->getContentType());
}
}

Next, define a route to access this controller method. Open your routes/web.php file and add the following route

use App\Http\Controllers\BarcodeController;

Route::get('/generate-qr-code', [BarcodeController::class, 'generateQRCode']);

Now, you can access the QR code generator via http://localhost:8000/generate-qr-code?url=your-url.

Generating Other Barcode Formats

For generating barcode formats other than QR codes, let’s create another method in our BarcodeController:

use Milon\Barcode\DNS1D;

public function generateBarcode(Request $request)
{
$barcodeText = $request->input('text'); // Get barcode text from request

// Generate barcode
$barcode = new DNS1D();
$barcode->setStorPath(__DIR__);
$barcodeImage = $barcode->getBarcodePNG($barcodeText, 'C39');

// Return barcode image as response
return response($barcodeImage, 200)
->header('Content-Type', 'image/png');
}

Add a new route in routes/web.php to access this method:

Route::get('/generate-barcode', [BarcodeController::class, 'generateBarcode']);

You can now generate barcode images by accessing http://localhost:8000/generate-barcode?text=your-text.

Conclusion: In this tutorial, we’ve learned how to generate barcodes in Laravel 11 using two different packages: “bacon/bacon-qr-code”