Backpack for Laravel charts, append sum to chart

318 Views Asked by At

As I'm trying to implement charts into Backpack for Laravel, I been stuck for a few hours on this problem. The following script gets the number of users created for each day and appends them to an array that is then shown on the charts.

        for ($days_backwards = 7; $days_backwards >= 0; $days_backwards--) {
        // Could also be an array_push if using an array rather than a collection.
        $users = Users::whereDate('created_at', today()->subDays($days_backwards))->count();
        $user[] = $users;
    }

Every iteration of the loop adds a number to the array (or is it a collection??) so something like [2,5,10,9,...].

I would rather like to get the total amount of users that ever registered, incrementally for each day, so that the result would be someting like [2,7,17,26,...].

I figured I could add each iteration with array_sum() but it's not working. Is it an array anyways? Is there a way to append to this list the sum to its previous?

1

There are 1 best solutions below

0
On

Well I kind of figured out!

$sum = 0;
for ($days_backwards = 7; $days_backwards >= 0; $days_backwards--) {
    $users  = Users::whereDate('created_at', today()->subDays($days_backwards))->count();
    $sum    = $sum + $users;
    $user[] = $sum;
}

It works!