laravel Sync命令不会更新值

bakd9h0s  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(173)

您好,我在Laravel中有此代码,用于更新现有的many-to-many关系表,因此当我使用sync命令时,值更新错误,代码如下:

public function update(Request $request, $id)
{
    $order = Order::where('id', $id)->first();

    $request->validate([
        'order_number' => 'required',
        'client_id' => 'required',
        'description' => 'required',
        'productOrder' => 'required',
        'productOrder.*.product_id' => 'required|distinct|exists:products,id',
        'productOrder.*.amount' => 'required|numeric|min:1',
    ]);

    $order->update($request->all());

    foreach ($request->productOrder as $product) {

        $values[] = [
            'order_id' => $order->id,
            'product_id' => $product['product_id'],
            'amount' => $product['amount'],
        ];

        $amount = Product::find($product['product_id']);

        $totalValue = $product['amount'] + $amount->amount;

        $amount->update(['amount' => $totalValue]);
    }
    
    $order->products()->sync($values); //the problem is here

    $orders = Order::all();

    $orders->load('client', 'products');

    return view('orders/index', compact('orders'));
}

如果i有两个值,如:

Product1 -> amount: 250

Product2 -> 100

我将这些现有值更新为

Product1 -> amount: 350

 Product2 -> 200

结果会是

Product2 -> 200

Product2 -> 200

如果我把这行代码写成dd($order->products()->sync($values));,我会得到这个结果

如果我添加一个新产品与现有的我得到这个结果

据我所知,第一个结果被第二个结果替换或删除,为什么会发生这种情况?

plicqrtu

plicqrtu1#

所以,你有这样的代码:

public function update(Request $request, $id)
{
    $order = Order::where('id', $id)->first();

    $request->validate([
        'order_number' => 'required',
        'client_id' => 'required',
        'description' => 'required',
        'productOrder' => 'required',
        'productOrder.*.product_id' => 'required|distinct|exists:products,id',
        'productOrder.*.amount' => 'required|numeric|min:1',
    ]);

    $order->update($request->all());

    foreach ($request->productOrder as $product) {

        $values[] = [
            'order_id' => $order->id,
            'product_id' => $product['product_id'],
            'amount' => $product['amount'],
        ];

        $amount = Product::find($product['product_id']);

        $totalValue = $product['amount'] + $amount->amount;

        $amount->update(['amount' => $totalValue]);
    }
    
    $order->products()->sync($values); //the problem is here

    $orders = Order::all();

    $orders->load('client', 'products');

    return view('orders/index', compact('orders'));
}

它的问题是sync得到错误的ID,因为$values具有数字自动增量ID:012等等。
您需要传递给sync的是一个ID数组[1, 2, 3],或者是数组的数组(当您想要更新与该ID相关的列时,例如:[1 => ['amount' => 100], 2, 3].
让我试着修改你的代码来帮助你。
假设您有orders表和products表。您还有一个名为order_product表的透视表,因此您将关系存储在其中。此order_product表具有:

  • id
  • order_id
  • product_id
  • amount

因此,您的代码应该如下所示:

public function update(Request $request, $id)
{
    $validated = $request->validate([
        'order_number' => 'required',
        'client_id' => 'required',
        'description' => 'required',
        'productOrder' => 'required',
        'productOrder.*.product_id' => 'required|distinct|exists:products,id',
        'productOrder.*.amount' => 'required|numeric|min:1',
    ]);

    $order = Order::find($id); // I would change this to use implicit binding

    $order->update($validated); // Don't use $request->all(), you are using unvalidated values

    foreach ($request->productOrder as $product) {

        $values[$product['product_id']] = [
            'amount' => $product['amount']
        ];

        $amount = Product::find($product['product_id']);

        $totalValue = $product['amount'] + $amount->amount;

        $amount->update(['amount' => $totalValue]);
    }
    
    $order->products()->sync($values); // Now it will work

    $orders = Order::all();

    $orders->load('client', 'products');

    return view('orders/index', compact('orders'));
}

相关问题