Hey!
Are you sure that reduce is the correct way to use this, as it uses the amount from the previous day for the next one and having items with 0 amount wouldn't make any sense, imho.
Otherwise you could use the day number as key and the amount as value and then fill it with missing items like this (untested):
$filledArray = $this->Orders
->groupBy(function($item){ return $item->created_at->format('d'); })
->mapWithKeys(function($group) {
$day = (int)$group->first()->created_at->format('d'); // parse as integer to remove leading zero
$amount = $group->sum('total_price');
return [$day => $amount]
})
->toArray() + array_fill(1, 31, 0);
collect($filledArray)->each(function ($amount, $day) use(&$columnChartModel) {
$columnChartModel->addColumn($day, $amount, '');
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community