Laravel 包含重复数据的集合总和
Laravel Sum on collection with duplicates data
我有一个包含多个数据的对象,因为 controlNumber
每个 controlNumber
都有 shipment.qty
我想要的是将每个相同 controlNumber
的 qty
加起来=] 并显示一个 controlNumber
和总数量。
合集在下方
"collection": [
{
"id": 983,
"controlNumber": 4498818,
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": 1638
}
},
{
"id": 982,
"controlNumber": 4498818,
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": 1638
}
},
{
"id": 936,
"controlNumber": 4498815,
"ItemNumber": "PS2T01096",
"shipment": {
"id": 11,
"Item": "PS2T01096",
"ColorCode": "MALDI",
"qty": 1212
}
},
]
controlNumber 4498818
有两个装运数据和两个 qty
我想要显示一个 controlNumber
总 qty
的 3276
我退休的是所有 controlNumber
中的 return 总数 qty
$result = $collection->pipe(function ($collection) {
return collect([
'shipment_qty' => $collection->sum('shipment.qty'),
]);
});
输出应该如下
"collection": [
{
"id": 983,
"controlNumber": 4498818,
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": 3276
}
},
{
"id": 936,
"controlNumber": 4498815,
"ItemNumber": "PS2T01096",
"shipment": {
"id": 11,
"Item": "PS2T01096",
"ColorCode": "MALDI",
"qty": 1212
}
},
]
您可以使用 unique 收集方法根据控制编号值仅获取唯一项目:
$uniqueCollection=$collection->unique('controlNumber');
$result=$collection2->sum(function ($item){
return $item->shipment->qty;
});
这样你就得到了一个集合,其中键是 controlNumber
,值是 shipment->qty
.
的总和
$collection->mapToGroups(function ($item) {
return [$item->controlNumber => $item->shipment->qty];
})->map->sum();
我有一个包含多个数据的对象,因为 controlNumber
每个 controlNumber
都有 shipment.qty
我想要的是将每个相同 controlNumber
的 qty
加起来=] 并显示一个 controlNumber
和总数量。
合集在下方
"collection": [
{
"id": 983,
"controlNumber": 4498818,
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": 1638
}
},
{
"id": 982,
"controlNumber": 4498818,
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": 1638
}
},
{
"id": 936,
"controlNumber": 4498815,
"ItemNumber": "PS2T01096",
"shipment": {
"id": 11,
"Item": "PS2T01096",
"ColorCode": "MALDI",
"qty": 1212
}
},
]
controlNumber 4498818
有两个装运数据和两个 qty
我想要显示一个 controlNumber
总 qty
的 3276
我退休的是所有 controlNumber
qty
$result = $collection->pipe(function ($collection) {
return collect([
'shipment_qty' => $collection->sum('shipment.qty'),
]);
});
输出应该如下
"collection": [
{
"id": 983,
"controlNumber": 4498818,
"ItemNumber": "PS2W12077",
"shipment": {
"id": 27,
"Item": "PS2W12077",
"ColorCode": "GRPFR",
"qty": 3276
}
},
{
"id": 936,
"controlNumber": 4498815,
"ItemNumber": "PS2T01096",
"shipment": {
"id": 11,
"Item": "PS2T01096",
"ColorCode": "MALDI",
"qty": 1212
}
},
]
您可以使用 unique 收集方法根据控制编号值仅获取唯一项目:
$uniqueCollection=$collection->unique('controlNumber');
$result=$collection2->sum(function ($item){
return $item->shipment->qty;
});
这样你就得到了一个集合,其中键是 controlNumber
,值是 shipment->qty
.
$collection->mapToGroups(function ($item) {
return [$item->controlNumber => $item->shipment->qty];
})->map->sum();