请求下一个请求后收到的响应



当请求更新产品时,它们在数据库中更新成功,但在api的响应中,例如,产品的总价格是100,当我更新它们时,它们必须是150,所以当我发送请求获得150时,我找到100,当我再次更新我们说200时,我找到150 ... .
这是我的更新函数

public function updateProducts(AdminUpdateOrderRequest $request, $id)
{
$orderValidated = $request->validated();
$order = Order::findOrFail($id);
if (isset($orderValidated['products'])) {
$order->products()->sync($orderValidated['products']);
}
DB::table('order_product')->where('order_id', $order->id)
->update([
'total_buy_price' => DB::raw('order_product.quantity * (select converted_buy_price from products where id = order_product.product_id)'),
'total_selling_price' => DB::raw('order_product.quantity * (select converted_selling_price from products where id = order_product.product_id) ')
]);
DB::table('orders')->where('id', $order->id)->update([
'cost_price' => DB::raw('(select SUM(order_product.total_buy_price) from order_product where order_product.order_id = ' . $order->id . ')'),
'total_price' => DB::raw('(select SUM(order_product.total_selling_price) from order_product where order_product.order_id = ' . $order->id . ')'),
'remaining' => DB::raw('(select SUM(order_product.total_buy_price) from order_product where order_product.order_id = ' . $order->id . ' - ' . $order->payments->sum('amount') . ')'),
'products_count' => DB::raw('(select SUM(quantity) from order_product where order_product.order_id = ' . $order->id . ')')
]);
DB::table('packages')->update([
'selling' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
'amount' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
'cost' => DB::raw('(select SUM(orders.cost_price) from orders where orders.package_id = packages.id)'),
]);
return OrderResource::make($order)->additional([
'success' => true,
]);
}

解决方案:更新查询后尝试获取产品查询。更新后的$order = Order::findOrFail($id);

我用这种方式更新订单,解决了这个问题

public function updateProducts(AdminUpdateOrderRequest $request, $id)
{
$orderValidated = $request->validated();
$order = Order::findOrFail($id);
if (isset($orderValidated['products'])) {
$order->products()->sync($orderValidated['products']);
}

DB::table('order_product')->where('order_id', $order->id)
->update([
'total_buy_price' => DB::raw('order_product.quantity * (select converted_buy_price from products where id = order_product.product_id)'),
'total_selling_price' => DB::raw('order_product.quantity * (select converted_selling_price from products where id = order_product.product_id) ')
]);

$order->cost_price = DB::table('order_product')->where('order_product.order_id', '=', $order->id)->sum('order_product.total_buy_price');
$order->total_price = DB::table('order_product')->where('order_product.order_id', '=', $order->id)->sum('order_product.total_selling_price');
$order->remaining = $order->total_price - $order->payments->sum('amount');
$order->products_count = DB::table('order_product')->where('order_product.order_id', '=', $order->id)->sum('order_product.quantity');
DB::table('packages')->update([
'selling' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
'amount' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
'cost' => DB::raw('(select SUM(orders.cost_price) from orders where orders.package_id = packages.id)'),
]);
return OrderResource::make($order)->additional([
'success' => true,
]);
}

相关内容

  • 没有找到相关文章

最新更新