Sagar Naliyapara Sagar Naliyapara - 4 years ago 454
SQL Question

laravel: how to multiply current value of column with some value when updating rows?

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

Answer Source

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'),
));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download