Laravel Query Builder Update with Increment

帅比萌擦擦* 提交于 2019-12-08 04:07:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!