I'm trying to get the total budgets of the last x days. I tried the following but the days without budgets are not being returned.
<?php
class Budget extends \Eloquent {
public static function getTotalBudgetsByDay($days = 31)
{
$budgetByDays = self::where('created_at', '>=', \Carbon\Carbon::now()->subDays($days))
->groupBy('date')
->orderBy('date', 'ASC')
->get([
\DB::raw('Date(created_at) as date'),
\DB::raw('COUNT(*) as total')
]);
return array_map(function ($row) {
return $row->getAttributes();
}, $budgetByDays->all());
}
}
$budgetsByDays = \Budget::getTotalBudgetsByDay();
Result:
Array (
[0] => Array
(
[date] => 2015-01-08
[total] => 2
)
[1] => Array
(
[date] => 2015-01-09
[total] => 1
)
[2] => Array
(
[date] => 2015-01-11
[total] => 7
)
)
in this case on 10 not appear in the array