Dec 14, 2022
0
Level 1
Laravel scope function for getting everyday data sum each day for a month or between 2 dates.
how to get scope function to get sum of data of each day for a month or between 2 dates. I tried this but does not work.
public function scopeEachDayInPeriod($query, array $dates){
$period = CarbonPeriod::create($dates[0], $dates[1]);
foreach ($period as $date) {
$day = $date->format('Y-m-d');
$days30[] =$day;
$kwh30days[] = Charge::whereDate('start', $day)->get()->sum('kWh');
$query->where('start',$day);
}
return $query->whereDate('start', '>=', $dates[0])
->whereDate('start', '<=', $dates[1]);
}
Please or to participate in this conversation.