我是否需要将sum(orderdets.quantity)中的变量插入另一个变量?
$order = Order::with('customer','product')
->select(
'orders.id',
'orders.customer_id',
'orderdets.product_id',
DB::raw('SUM(orderdets.quantity)'))
->get();
答案 0 :(得分:1)
尝试一下
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
dd($orders);
答案 1 :(得分:0)
使用 AS tempName
通过特定名称获取列数据。这样就可以访问它。
$order = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) AS sumOrders'))->get();
答案 2 :(得分:0)
$orders = Order::with('customer','product')
->select('orders.id', 'orders.customer_id', 'orderdets.product_id'))
->get()->map(function($order){
$order->put('sum', $order->orderdets->sum('quantity');
})->save();
这将汇总您的所有债务,并一次性更新订单表,虽然您的问题尚不清楚,但我认为您必须在寻找类似的东西
答案 3 :(得分:0)
请尝试在控制器中使用以下脚本。
$ orders = Order :: with('customer','product')-> select('orders.id','orders.customer_id','orderdets.product_id',DB :: raw('SUM(orderdets .quantity)as sum'))-> get();
返回$ orders;
谢谢 PHPanchal