首页 > 解决方案 > 如何计算laravel中的订单总和?

问题描述

如果我的 order_id 为 15,小计为 10 美元,20 美元属于 Seller_id 1,另一个 order_id 为 15,小计为 15 美元,属于 Seller_id 2。我怎样才能得到每个卖家的总数。

现在我得到了整个 15 个订单的总和,即使它有不同的卖家 ID。这是表格的样子: 在此处输入图像描述

这是我现在计算总数的逻辑

$product = product::find($productId);
           OrderProduct::create([
            'quantity' => $item['quantity'],
            'Subtotal' =>$item['pro_price'] * $item['quantity'],
            'total' => $total += $item['pro_price'] *   $item['quantity'],

这是刀片

@foreach ($order->orderItems as $item)
@if($item->product->user_id == $userID)
    <td>{{ $item->product->Subtotal }}</td>
    <td>{{ $item->$totals }}</td>
@endif
@endforeach

任何帮助,将不胜感激。

标签: phplaravellaravel-5eloquent

解决方案


您可以使用 SQL:

$totals = OrderProduct::select("seller_id", DB::Raw("SUM(Subtotal) AS sub_total"))
    ->groupBy('seller_id')
    ->get();

推荐阅读