Martney Acha
Martney Acha

Reputation: 3002

Laravel Query Builder Update with Increment

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]);

Upvotes: 3

Views: 6079

Answers (1)

rikardo_paiva
rikardo_paiva

Reputation: 393

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);

Upvotes: 9

Related Questions