Home » Php » php – get current value of column query builder laravel

php – get current value of column query builder laravel

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am using Laravel and I want to update via query builder

here is my query

DB::table('cart_product')->where([['cart_id', '=', $cart->id], ['product_id', '=', $productId]])->update(['quantity' => $quantity, 'total_price' => $productPrice * $quantity, 'specification_price' => $currentSpecification_price* $quantity]);

so I want to multiply $quantity with current value of this column
How can I access to $currentSpecification_price I mean current value of column?

How to&Answers:

Use DB::raw('column_name') to get the current column value like this:

DB::table('cart_product')
   ->where([['cart_id', '=', $cart->id], ['product_id', '=', $productId]])
   ->update(['quantity' => $quantity, 
             'total_price' => $productPrice * $quantity, 
             'specification_price' => DB::raw('specification_price') * $quantity]);