Sabyasachi Ghosh
Sabyasachi Ghosh

Reputation: 1500

How to multiply two columns from two different table with eloquent in laravel

order

currency

order_list

I want the following output:

Here I want to multiply the ex_rate_with_base field of order table with unit_price of order_list table using eloquent.

Upvotes: 4

Views: 7011

Answers (2)

Benjamin Brasseur
Benjamin Brasseur

Reputation: 558

If you have Order and OrderList Eloquent Models, you can try something like this from a controller.

YourOrderController.php

$order= Order::find($orderId);
$orderList = OrderList::where('order_id',$orderId)->get();
$calculatedPrice = $orderList->unit_price_in_base_currency * $order->ex_rate_with_base;

Upvotes: 1

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think you can try this:

DB::table('order')
->select(DB::raw('(order.ex_rate_with_base * order_list.unit_price_in_base_currency) as orderRate'))
->leftjoin('order_list','order_list.order_id','=','order.id)
->get();

Hope this work for you !!!

Upvotes: 4

Related Questions