Support the ongoing development of Laravel.io →
Eloquent
Last updated 3 months ago.
0

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, '');
}
Last updated 9 months ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

Sagi S Sagi S shimsag Joined 5 Sep 2021

Moderators

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.

© 2022 Laravel.io - All rights reserved.