我正在使用命令schuduler将这些订单状态从“新”更新为“取消”并每分钟运行一次,更新功能正在运行,但是问题是每次您将“新”更新为“取消”时,我都有另一种情况将订购产品的数量增加或增加回产品库存中
$getNeworders = Order::with('products')
->where('order_status', '=' ,'New')
->whereDate('created_at' , '<' ,Carbon::now()->subMinutes(1440))->get();
//1440minutes in 24hrs
foreach($getNeworders as $try) {
$gettheid= $try->id;
Order::where(['id'=>$gettheid])->update(['order_status' =>'Cancelled']);
}
foreach($getNeworders as $increments){
foreach($increments->product() as $incrementt)
$idd= $incrementt->product_id;
$qty= $incrementt->pivot()->quantity;
Product::where(['id' => $idd])->decrement(['stock' => $qty]);
//更新正常,但是下面的增量语句不起作用
答案 0 :(得分:0)
foreach($increments->product() as $incrementt)
缺少大括号。另一件事是increment/decrement
不接受数组,但接受列名和值。
是这样的:
foreach($getNeworders as $increments){
foreach($increments->product() as $incrementt){
$idd= $incrementt->product_id;
$qty= $incrementt->pivot()->quantity;
Product::where(['id' => $idd])->decrement('stock', $qty);
}
}