Laravel 9 - How to generate Bar Code, Qr Code, Codabar etc

Touseef Afridi
01 Sep 24

Laravel 9 - How to generate Bar Code, Qr Code, Codabar etc

In this tutorial, we’ll explore generating various codes, including barcodes, QR codes, and Codabar in Laravel 9 for product labeling and information access.


If you're a video person, feel free to skip the post and check out the video instead!


Step # 1 : Create fresh Laravel project or use an existing Laravel project.

Two commands to create fresh Laravel project.
Global Command : laravel new barcode
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist barcode

Step # 2 : Access the project.

Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/barcode
Run the Laravel Vite development server. Install the required dependencies and start the Vite server for front-end assets.
Command : npm install && npm run dev
Open a new Git Bash window or tab, and navigate to the same project directory to run further Laravel commands.

Step # 3 : Install barcode package.

Command : composer require milon/barcode

Step # 4 : Publish the configuration file (Optional).

Command : php artisan vendor:publish --provider="Milon\Barcode\BarcodeServiceProvider"

Step # 5 : Create a controller.

Command : php artisan make:controller ProductController
Make a method named index to return a view.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
    public function index()
    {
     return view('barcode');
    }
}

Step # 6 : Create a view.

In order to display the Codes create a view named barcode.blade.php.
(resources/views/barcode.blade.php)
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Laravel Generate Barcode Examples - Code Shotcut</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
    <div class="container mt-4">
        <h1>Code Shotcut - Bar Code, QR Code etc</h1>
        <!-- Pass product code as the 1st parameter -->
        <div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA') !!}</div>
        <div class="mb-3">{!! DNS2D::getBarcodeHTML('', 'QRCODE') !!}</div>
        <div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA2T') !!}</div>
        <div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'CODABAR') !!}</div>
        <div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'KIX') !!}</div>
        <div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'RMS4CC') !!}</div>
        <div class="mb-3">{!! DNS1D::getBarcodeHTML('4445645656', 'UPCA') !!}</div>
    </div>
</body>
</html>

Step # 7 : Create a route.

Import ProductController class
use App\Http\Controllers\ProductController;
Create a route
Route::get('/barcode', [ProductController::class, 'index']);

Step # 8 : It's time to test.

Run the Laravel server.
Command : php artisan serve
Access url 127.0.0.0:8000/barcode



Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering