How is the format for January, February etc? 1 or 01?
Jul 22, 2022
5
Level 3
Group Collection by month
Hi guys,
I have a json of transactions, in each transaction there is amount and date. I want to group all the results by month. For example I want a result like this:
{
month: 22-11 (november)
amount: x$
}
Level 102
@Kris01 ok something like this
$x = collect($data)->map(function($item) {
$item['amount'] = str_replace(' usd', '', $item['amount']) ;
return $item;
})->groupBy(function ($item) {
return \Carbon\Carbon::parse($item['date'])->format('Y m');
})->map->sum('amount')
->map(function ($sum, $date) {
return [
'month' => $date,
'amount' => $sum
];
});
Please or to participate in this conversation.