Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

debob's avatar
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]);
}
0 likes
0 replies

Please or to participate in this conversation.