Reputation: 1911
I want to count all register users each day from past 30 days. If there is no registration return 0
Currently i'm using this
$startDate = Carbon\Carbon::now()->subDays(30);
$signup = User::where('created_at', '>=', $startDate)
->groupBy('date')
->orderBy('date', 'DESC')
->get([
DB::raw('Date(created_at) as date'),
DB::raw('COUNT(*) as value')
]);
return $signup;
Current output
[
{
"date" : "2014-05-29",
"value" : 1
},
{
"date" : "2014-05-15",
"value" : 1
}
]
If there is no registration then display value 0
for example
[
{
"date" : "2014-05-29",
"value" : 1
},
{
"date" : "2014-05-28",
"value" : 0
},
{
"date" : "2014-05-27",
"value" : 0
},
{
.....
.....
},
{
"date" : "2014-05-15",
"value" : 1
}
{
"date" : "2014-05-14",
"value" : 0
}
]
How can I get such result.
Upvotes: 0
Views: 125
Reputation: 86
Put the $result array in another array value by value, and if lastrowdate - rowdate > 1 day, use this function to fill up the empty dates: https://stackoverflow.com/a/4312491/3696548
Upvotes: 1