简体   繁体   English

如何将两个不同的表中的两列与laravel中的雄辩相乘

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

order 订购

  • id ID
  • currency_code 货币代码
  • ex_rate_with_base ex_rate_with_base
  • status 状态
  • total_in_base_currency total_in_base_currency
  • status 状态

currency 货币

  • currency_code 货币代码
  • symbol 符号
  • currency_name CURRENCY_NAME
  • currency_value CURRENCY_VALUE
  • ex_rate_with_base ex_rate_with_base
  • status 状态

order_list 订单

  • id ID
  • order_id ORDER_ID
  • product_code 产品代码
  • qty 数量
  • unit_price_in_base_currency unit_price_in_base_currency
  • status 状态

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. 在这里,我要乘ex_rate_with_base 顺序表的字段unit_price用雄辩order_list表。

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 !!! 希望这对你有用!!!

If you have Order and OrderList Eloquent Models, you can try something like this from a controller. 如果你有Order和OrderList Eloquent Models,你可以从控制器尝试这样的东西。

YourOrderController.php 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM