stackoverflow,
I have working laravel function which get the daily sum of sales for the last 30 days. I will use the data to build a graph, so I need to get the dates even if its empty and give them a value of "0" as their sum.
here's my code (it's working but only returns dates which are not empty)
public function getDaily() {
$startDate = Carbon::now()->subDays(30);
$endDate = Carbon::now();
$all_dates = array();
for($i = 0;$i<=30;$i++)
{
$all_dates[] = $startDate->toDateString();
$startDate->addDay();
$sales=DB::table('sale_details')
->select(DB::raw('sum(amount_due) as daily'),DB::raw('date(created_at) as date'))
->groupBy('date')
->orderBy('date','desc')
->get();
}
return $sales;
}