Tạo mã vạch trong Laravel rất quan trọng đối với các ứng dụng quản lý kho, theo dõi sản phẩm hoặc bất kỳ hoạt động kinh doanh nào yêu cầu mã định danh duy nhất. Laravel cung cấp cách dễ dàng để tạo mã vạch bằng thư viện milon/barcode. Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách tạo mã vạch trong Laravel từng bước.

1. Cài đặt dự án Laravel

Trước tiên, nếu bạn chưa có dự án Laravel, hãy tạo một dự án mới bằng lệnh sau:

shell
composer create-project laravel/laravel demo-codetuthub

Di chuyển vào thư mục dự án:

shell
cd demo-codetuthub

2. Cài đặt thư viện barcode

Để tạo mã vạch trong Laravel, chúng ta sẽ sử dụng gói milon/barcode. Cài đặt nó bằng Composer:

shell
composer require milon/barcode

Xem thêm các version mà milon/barcode hỗ trợ: https://github.com/milon/barcode

Sau khi cài đặt hoàn tất, Laravel sẽ tự động nhận diện thư viện này.

3. Tạo mã vạch, QR trong giao diện Blade

Sau khi cài đặt gói milon/barcode, bạn có thể tạo mã vạch trực tiếp trong Blade bằng cú pháp sau:

html
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel - CodeTutHub</title>

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.bunny.net">
    <link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet"/>
</head>
<body class="font-sans antialiased dark:bg-black dark:text-white/50">
<h2>Ví dụ mã vạch</h2>
<p>Mã vạch EAN-13:</p>
{!! DNS1D::getBarcodeHTML('123456789012', 'EAN13') !!}

<p>Mã QR Code:</p>
{!! DNS2D::getBarcodeHTML('https://codetuthub.com', 'QRCODE') !!}
</body>
</html>

Giải thích:

  • DNS1D::getBarcodeHTML('123456789012', 'EAN13') tạo mã vạch 1D loại EAN-13.
  • DNS2D::getBarcodeHTML('https://codetuthub.com', 'QRCODE') tạo mã QR Code.

4. Tạo mã vạch, QR code trong controller

Bạn cũng có thể tạo mã vạch trong controller của Laravel và truyền nó đến view. Đầu tiên, tạo một controller mới:

shell
php artisan make:controller BarcodeController

Sửa đổi file app/Http/Controllers/BarcodeController.php như sau:

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Milon\Barcode\Facades\DNS1DFacade;
use Milon\Barcode\Facades\DNS2DFacade;

class BarcodeController extends Controller
{
    public function generate()
    {
        $barcode = DNS1DFacade::getBarcodeHTML('123456789012', 'EAN13');
        $qrcode = DNS2DFacade::getBarcodeHTML('https://codetuthub.com', 'QRCODE');

        return view('barcode', compact('barcode', 'qrcode'));
    }
}

5. Định nghĩa route

Thêm một route vào routes/web.php để hiển thị mã vạch:

php
<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/barcode', [\App\Http\Controllers\BarcodeController::class, 'generate']);

6. Tạo view file

Tạo file Blade mới resources/views/barcode.blade.php và thêm dữ liệu mã vạch vào:

php
resources/views/barcode.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Mã vạch được tạo - CodeTutHub</title>
</head>
<body>
    <h2>Mã vạch được tạo</h2>
    <p>{!! $barcode !!}</p>
    <h2>Mã QR Code được tạo</h2>
    <p>{!! $qrcode !!}</p>
</body>
</html>

7. Kiểm tra tạo mã vạch

Khởi chạy máy chủ Laravel:

shell
php artisan serve

Bây giờ, truy cập http://127.0.0.1:8000/barcode trong trình duyệt để xem mã vạch và QR code được tạo.

8. Lưu mã vạch thành ảnh

Nếu bạn muốn lưu mã vạch dưới dạng hình ảnh thay vì hiển thị trong HTML, hãy sử dụng đoạn mã sau:

php
use Milon\Barcode\Facades\DNS1D;
use Milon\Barcode\Facades\DNS2D;
use Illuminate\Support\Facades\Storage;

$barcode = DNS1D::getBarcodePNGPath('123456789012', 'EAN13');
Storage::put('barcodes/barcode.png', file_get_contents(public_path($barcode)));

Lệnh trên sẽ lưu hình ảnh mã vạch vào thư mục storage/app/barcodes/.

9. Kết luận

Tạo mã vạch trong Laravel rất đơn giản với gói milon/barcode. Hướng dẫn này đã giúp bạn hiểu:

  • Cách cài đặt Laravel và thư viện milon/barcode.
  • Cách tạo mã vạch và QR code trong giao diện Blade.
  • Cách sử dụng controller để tạo mã vạch động.
  • Cách định nghĩa route và lưu mã vạch dưới dạng hình ảnh.

Bây giờ, bạn có thể tích hợp tính năng tạo mã vạch vào ứng dụng Laravel của mình để phục vụ quản lý kho, theo dõi sản phẩm hoặc bất kỳ nhu cầu kinh doanh nào.

Source code: https://github.com/vantoantg/laravel-11-demo/tree/create-BarCode