Php 从集合中仅获取唯一项,并将其他值相加到一个项

Php 从集合中仅获取唯一项,并将其他值相加到一个项,php,laravel,collections,Php,Laravel,Collections,我确实有这个拉威尔收藏 Collection {#239 ▼ #items: array:2 [▼ 0 => array:21 [▼ "dueDate" => "2017-09-29" "groupByCode" => "REMINDER" "date" => "2017-09-29" "number" => "9030014" "

我确实有这个拉威尔收藏

    Collection {#239 ▼
      #items: array:2 [▼
        0 => array:21 [▼
          "dueDate" => "2017-09-29"
          "groupByCode" => "REMINDER"
          "date" => "2017-09-29"
          "number" => "9030014"
          "status" => "Reminder"
          "currencyCode" => "kr"
          "amount" => 1745.0
          "remainingAmount" => 1745.0

        ],
  2 => array:21 [▼
          "dueDate" => "2017-09-29"
          "groupByCode" => "REMINDER"
          "date" => "2017-09-29"
          "number" => "9030014"
          "status" => "Reminder"
          "currencyCode" => "kr"
          "amount" => 1345.0
          "remainingAmount" => 1745.0

        ],
        3 => array:21 [▼
          "dueDate" => "2017-09-29"
          "groupByCode" => "INVOICE"
          "date" => "2017-09-29"
          "number" => "9030026"
          "status" => "invoice"
          "currencyCode" => "kr"
          "amount" => 2389.0
          "remainingAmount" => 2389.0

        ]
      ]
    }
现在我的问题是如何根据状态只获取唯一的项目,例如上面我有两个具有状态提醒的项目,相反,我希望保持相同的收集格式,但作为回报,只有一个具有状态“提醒”的项目和所有现有键,总和将是两个提醒的总和

我曾尝试使用'where('status','rementer')->first(),但不起作用,因为它只需要firts元素

所以输出应该是这样的:

    Collection {#239 ▼
      #items: array:2 [▼
        0 => array:21 [▼
          "dueDate" => "2017-09-29"
          "groupByCode" => "REMINDER"
          "date" => "2017-09-29"
          "number" => "9030014"
          "status" => "Reminder"
          "currencyCode" => "kr"
          "amount" => TOTAL OF BOTH REMINDERS
          "remainingAmount" => 1745.0

        ],
  2 => array:21 [▼
          "dueDate" => "2017-09-29"
          "groupByCode" => "INVOICE"
          "date" => "2017-09-29"
          "number" => "9030014"
          "status" => "Invoice"
          "currencyCode" => "kr"
          "amount" => 2312 
          "remainingAmount" => 1745.0

            ],
}
谢谢

代码如下:

$invoices = $this->boatService->getInvoices(cleanSsn(session('ssn')));

$invoices = collect($invoices)->whereIn('status', ['Reminder', 'Invoice']); 

您可以进行原始查询以汇总项目并按状态对其进行分组。试试这个:

$collection = \DB::table('invoices')
                 ->select(\DB::raw('sum(amount) as amount_total, status'))
                 ->groupBy('status')
                 ->get();

还分享您的尝试?@user2486 ok再次检查我刚才想的是
groupBy('groupByCode')
groupBy('status')
这是api服务提交的一个小更正,可能他需要求和而不是计数