问题
Hi I need to fix my query where I will need to update my product quantity with just incrementing it, here is my code with sample.
DB::table('product_warehouse')
->where('product_id', $product_id)
->where('warehouse_id', $warehouse_id)
->update(['product_qty' => $old_qty+$product_qty]);
回答1:
You can try both ways:
DB::table('product_warehouse')
->where('product_id', $product_id)
->where('warehouse_id', $warehouse_id)
->update(['product_qty' => DB::raw('product_qty + ' . $product_qty)]);
Or
DB::table('product_warehouse')
->where('product_id', $product_id)
->where('warehouse_id', $warehouse_id)
->increment('product_qty', $product_qty);
来源:https://stackoverflow.com/questions/45951180/laravel-query-builder-update-with-increment