Grouping returned collection by a date – Laravel

  Kiến thức lập trình

I’ve got this function:

public function getOverdue()
{
    $jobs = Job::where('RequiredTime', '<', Carbon::now()->addDays(10)->endOfDay())
        ->where('JobCancelled', '=', 0)
        ->where('JobCompleted', '=', 0)
        ->where('Ref9', '=', null)
        ->where('CreateDateTime', '>=', Carbon::now()->startOfMonth())
        //->groupBy('RequiredTime')
        ->orderBy('RequiredTime', 'asc')
        ->get();
    
    return view('pages.dispatch.overdue', [
        'title' => 'Overdue',
        'page' => 'Overdue',
        'js_action' => 'dispatch|overdue',
        'jobs' => $jobs
    ]);
}

It works as I’d expect in terms of returning exactly what I require, however I’ve tried numerous ways of grouping on the RequiredTime field. This field returns data as this:

Jun 3 2024 05:00:00:000PM

Which is fine, however I’d like to group the results insofar as if I have 120 results, across 5 different days, I get returned a collection of 5 items with results within each. I’ve tried casting the date as specific date formats, using DB::Raw() functions but to no avail. I just get thrown SQL errors.

Could anyone point me in the right direction?

LEAVE A COMMENT