我需要模型,它们按月分组:
$cars= $company->car->groupBy(function($item)
{
return Carbon::parse($item->created_at)->format('M');
});
$boats= $company->boat->groupBy(function($item)
{
return Carbon::parse($item->created_at)->format('M');
});
所以我收到了两个集合
Collection {#841 ▼
#items: array:3 [▼
"Jan" => Collection {#848 ▼
#items: array:1 [▼
0 => Cars {#859 ▶}
]
}
"Fab" => Collection {#852 ▶}
"Mar" => Collection {#853 ▶}
]
}
对于船只而言,我的问题是如何合并它们以获得类似的东西:
Collection {#841 ▼
#items: array:3 [▼
"Jan" => Collection {#848 ▼
#items: array:1 [▼
0 => Cars {#859 ▶}
1 => Boats {#860 ▶}
]
}
"Fab" => Collection {#852 ▶}
"Mar" => Collection {#853 ▶}
]
}
答案 0 :(得分:1)
您需要首先合并这两个集合,然后按月分组。
以下代码可以解决这个问题:
$result = $company->car->merge($company->boat)->groupBy(function($item) {
return Carbon::parse($item->created_at)->format('M');
});
答案 1 :(得分:0)
我是这样做的:
wait()