You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目中如何为产品创建并存储二维码图片路径/URL至数据库

关于Laravel二维码产品详情功能的问题

我正在开发一个Laravel项目,核心功能是扫描产品二维码获取产品详情,预设流程如下:

  • 使用Milon/Barcode生成唯一product_code并生成对应二维码
  • 将二维码图片的路径/URL与产品关联,存储到数据库
  • 用Instascan.Scanner扫描二维码
  • 从扫描内容中提取product_code,通过AJAX请求后端
  • 查询数据库匹配产品
  • 返回产品详情响应给前端

目前已实现二维码生成功能,现有代码如下:

现有代码

ProductController 存储逻辑

public function ProductStore(Request $request){
        
    // Generate the product code
    $product_code = uniqid();

    // Generate PNG path for the QR code
    $qrCodePath = DNS2D::getBarcodePNGPath($product_code, 'QRCODE');

    Product::insert([
        'name' => $request->name,
        'supplier_id' => $request->supplier_id,
        'unit_id' => $request->unit_id,
        'category_id' => $request->category_id,
        'quantity' => '0',
        'product_code' => $qrCodePath,  // 当前存储的是二维码路径
        'created_by' => Auth::user()->id,
        'created_at' => Carbon::now(), 
    ]);
        
    $notification = array(
        'message' => 'Product Inserted Successfully', 
        'alert-type' => 'success'
    );

    return redirect()->route('product.all')->with($notification); 

} // End Method 

视图展示代码

@foreach($product as $key => $item)
<tr>
    <td>{{ $key + 1 }}</td>
    <td>{{ $item->name }}</td>
    <td>{{ $item['supplier']['name'] }}</td>
    <td>{{ $item['unit']['name'] }}</td>
    <td>{{ $item['category']['name'] }}</td>
    <td>
        <!-- Link to download the QR code image -->
        <a href="{{ DNS2D::getBarcodePNGPath($item->product_code, 'QRCODE') }}" download="{{ $item->product_code }}">
            <!-- QR code image -->
            <img src="{{ DNS2D::getBarcodePNGPath($item->product_code, 'QRCODE') }}" alt="QR Code" width="100" height="100" />
            <br>p - {{ $item->product_code }}
        </a>
    </td>
@endforeach

当前数据库中product_code字段存储格式示例:\65cc0051d5c4bqrcode.png,现在需要解决的核心问题是:如何正确为每个产品创建并存储二维码路径/URL,以支持后续通过AJAX请求获取产品详情。


解决方案

1. 调整数据库字段结构

当前你把二维码路径存在了product_code字段,导致扫描后拿到的是路径而非产品唯一标识,无法直接匹配产品。需要拆分两个字段:

  • product_code:存储唯一产品编码(如你生成的uniqid())
  • qr_code_path:存储二维码图片的路径/URL

首先生成迁移文件添加字段:

php artisan make:migration add_qr_code_path_to_products_table --table=products

编辑迁移文件:

public function up()
{
    Schema::table('products', function (Blueprint $table) {
        $table->string('qr_code_path')->nullable();
    });
}

public function down()
{
    Schema::table('products', function (Blueprint $table) {
        $table->dropColumn('qr_code_path');
    });
}

执行迁移:

php artisan migrate

2. 修正控制器存储逻辑

将产品编码和二维码路径分开存储:

public function ProductStore(Request $request){
        
    // 生成唯一产品编码
    $product_code = uniqid();

    // 生成二维码PNG路径
    $qrCodePath = DNS2D::getBarcodePNGPath($product_code, 'QRCODE');

    Product::insert([
        'name' => $request->name,
        'supplier_id' => $request->supplier_id,
        'unit_id' => $request->unit_id,
        'category_id' => $request->category_id,
        'quantity' => '0',
        'product_code' => $product_code,  // 存储唯一产品编码
        'qr_code_path' => $qrCodePath,    // 存储二维码路径
        'created_by' => Auth::user()->id,
        'created_at' => Carbon::now(), 
    ]);
        
    $notification = array(
        'message' => 'Product Inserted Successfully', 
        'alert-type' => 'success'
    );

    return redirect()->route('product.all')->with($notification); 

} // End Method 

3. 修正视图展示逻辑

基于产品编码生成二维码,同时展示正确的产品标识:

@foreach($product as $key => $item)
<tr>
    <td>{{ $key + 1 }}</td>
    <td>{{ $item->name }}</td>
    <td>{{ $item['supplier']['name'] }}</td>
    <td>{{ $item['unit']['name'] }}</td>
    <td>{{ $item['category']['name'] }}</td>
    <td>
        <!-- 下载二维码链接 -->
        <a href="{{ DNS2D::getBarcodePNGPath($item->product_code, 'QRCODE') }}" download="{{ $item->product_code }}_qrcode.png">
            <!-- 展示二维码 -->
            <img src="{{ DNS2D::getBarcodePNGPath($item->product_code, 'QRCODE') }}" alt="QR Code" width="100" height="100" />
            <br>p - {{ $item->product_code }}
        </a>
    </td>
@endforeach

4. 扫描后AJAX获取详情逻辑

扫描二维码后得到的内容就是product_code,直接用该编码发起请求:

// 假设扫描得到的编码为scannedCode
$.ajax({
    url: '/product/detail',
    method: 'GET',
    data: { product_code: scannedCode },
    success: function(response) {
        // 处理并展示产品详情
        console.log(response.data);
    },
    error: function() {
        alert('未找到对应产品');
    }
});

添加后端路由(web.php):

Route::get('/product/detail', [ProductController::class, 'getProductDetail']);

新增控制器方法处理请求:

public function getProductDetail(Request $request)
{
    $product = Product::where('product_code', $request->product_code)->first();
    
    if (!$product) {
        return response()->json(['status' => 'error', 'message' => '产品不存在'], 404);
    }
    
    return response()->json([
        'status' => 'success',
        'data' => $product
    ]);
}

关键说明

  • 拆分字段后,扫描二维码直接拿到产品唯一编码,查询逻辑更清晰可靠
  • 如果需要存储二维码的完整URL而非本地路径,可以用url(DNS2D::getBarcodePNGPath($product_code, 'QRCODE'))生成后存入qr_code_path

内容的提问来源于stack exchange,提问作者acha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:10:35