如何在 Laravel 中将条形码存储在验证请求中

How can I store barcode inside validate request in Laravel

我有一个存储产品详细信息的商店功能代码。然而,条形码总是return空的。我不知道如何用 Product::create($data);' 保存它。能问一下路吗?

$data = new Product;
$data->name = $request->name;

像上面这样的对象需要一个一个保存吗?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use Picqer;

class ProductsController extends Controller
{
    public function index() {
        $products = Product::orderBy('id')->get();
        return view('products.index', compact('products'));
    }

    public function show(Product $product) {
        return view('products.show', ['product' =>  $product]);
    }
    
    public function create() {
        $products = Product::all();
        return view('products.create', compact('products'));
    }

    public function edit(Product $product) {
        return view('products.edit', compact('product'));
    }

    public function update(Request $request, Product $product) {
        $request->validate([
            'code' => 'required',
            'barcode' => 'required',
            'name' => 'required',
            'description' => 'required|string|max:250',
            'unit' => 'required|int',
            'cost' => 'required',
            'srp' => 'required',
            'supplier' => 'required|string|max:250',
            'quantity_left' => 'required',
            'category' => 'required|string|max:250',
            'delivered_date' => 'required|date',
            'expiration_date' => 'required|date'
        ]);
        $product->code = $request->code;
        $product->barcode = $request->barcode;
        $product->name = $request->name;
        $product->description = $request->description;
        $product->unit = $request->unit;
        $product->cost = $request->cost;
        $product->srp = $request->srp;
        $product->supplier = $request->supplier;
        $product->quantity_left = $request->quantity_left;
        $product->category = $request->category;
        $product->delivered_date = $request->delivered_date;
        $product->expiration_date = $request->expiration_date;
        $product->save();
        return redirect()->route('products.index');
    }

    public function store(Request $request) {
        $code = $request->code;
        $generator = new Picqer\Barcode\BarcodeGeneratorHTML();
        $barcode = $generator->getBarcode($code, $generator::TYPE_CODE_128);
        
        $data = $request->validate([
            'code' => 'required',
            'barcode' => 'max:15|nullable',
            'name' => 'required',
            'description' => 'required|string|max:250',
            'unit' => 'required|int',
            'cost' => 'required',
            'srp' => 'required',
            'supplier' => 'required|string|max:250',
            'quantity_left' => 'required',
            'category' => 'required|string|max:250',
            'delivered_date' => 'required|date',
            'expiration_date' => 'required|date'
        ]);


        Product::create($data);

        return redirect()->route('products.index')->with('success', 'Product has been added!');
    }

    public function destroy(Product $product) {
        $product->delete();
        return back();
    }
}

我想我错过了什么。我用过这个框架:https://github.com/picqer/php-barcode-generator

您需要用您生成的条码覆盖来自请求的条码。


    $product = new Product();
    $product->name = 'some name';
    ...
    $product->barcode = $barcode;
    $product->save();

我通过合并两个数组完成了这项工作:

$data = array_merge($data, ['barcode' => $barcode]);
Product::create($data);