Support the ongoing development of →
posted 2 years ago
Last updated 2 years ago.


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, '');
Last updated 2 years ago.

Sign in to participate in this thread!


Your banner here too?

Sagi S shimsag Joined 5 Sep 2021


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.