Ad

Laravel: How To Multiply Current Value Of Column With Some Value When Updating Rows?

- 1 answer

Problem:

I want to update two column of mysql table,

column1 decrement with some value and column2 multiplication with some value,

I have done about decrement like this from here

Tried:

$stock_obj->decrement('quantity_on_hand', $product_data["quantity"], array(
    'total_quantity_on_hand' => 'quantity_on_hand' * some_other_value
));

Here decrement working but how can i do multiplication?

I have that solution in CI(CodeIgniter) but don't know how to does it in laravel any help would be appreciated...

CI(CodeIgniter) WAY:

$this->db->set("quantity_on_hand", "quantity_on_hand-" . ($product_data["quantity"] ? $product_data["quantity"] : 0), FALSE);
$this->db->set("total_quantity_on_hand", "quantity_on_hand*" . ($product_data['product_packing_value'] ? $product_data['product_packing_value'] : 1), FALSE);
$this->db->where("id", $warehouse_transfer_product_data['stock_id'])->update("stock");
Ad

Answer

The following should do the trick:

DB::table('your_table')
->where('some_column', $someValue)
->update(array(
    'column1' => DB::raw('column1 * 2'),
    'column2' => DB::raw('column2 - 1'),
));
Ad
source: stackoverflow.com
Ad