'Laravel . How to Get the start_date end_date of month, week or day of closed_at date in a filter

How to Get the start_date end_date of month, week or day of closed_at date in a filter. i want to fetch start_date an end_date of closed_at The two dates specify the beginning and the end of the period (day, week or month) that has been grouped by'closed_at'

public function scopeGroups($query, $period)
    {
        switch ($period) {
            case 'day':
                $query->groupBy(DB::raw("DATE_FORMAT(closed_at, '%d')"));
                break;

            case 'week':
                $query->groupBy(DB::raw("DATE_FORMAT(closed_at, '%u')"));
                break;

            case 'month':
                $query->groupBy(DB::raw("DATE_FORMAT(closed_at, '%m')"));
                break;
        }
    }

}


Solution 1:[1]

Okay here is what I think youre looking for

public function scopeGroups($query, $period)
{
    $now = Carbon::now();
    switch ($period) {
        case 'day':
            $query->whereBetween('closed_at', [$now->startOfDay(), $now->endOfDay()]);
            break;

        case 'week':
            $query->whereBetween('closed_at', [$now->startOfWeek(), $now->endOfWeek()]);
            break;

        case 'month':
            $query->whereBetween('closed_at', [$now->startOfMonth(), $now->endOfMonth()]);
            break;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dz0nika