php - 如何计算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
任何帮助,将不胜感激。
解决方案
您可以使用 SQL:
$totals = OrderProduct::select("seller_id", DB::Raw("SUM(Subtotal) AS sub_total"))
->groupBy('seller_id')
->get();
推荐阅读
- javascript - 在 javascript 中实现 normalize() 到搜索功能中
- linux - 当前用于解析 HTML 的 sed 命令有什么问题?
- php - imagick-3.4.4安装在make时抛出错误:grep:/opt/bitnami/common/lib/libfreetype.la:没有这样的文件或目录
- c# - 通过嵌套元组 C# 中的项目进行引用
- python - 使用 enumerate 计算一个值在 np 数组中的位置
- php - PHP session_start 返回 false,$_SESSION 变量不是持久的
- git - 为什么我的 git diff 在@@ 之后显示评论
- python - 在 django 中为用户创建公共配置文件
- python - 反向波兰符号算法不能与相同的操作数正确工作
- html - WKWebView 画布问题。画布内的链接没有做任何事情