skip to Main Content

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?

2

Answers


  1. use closure function to further group the result as your need, i guess
    it would look like

    $groupedJobs = $jobs->groupBy(function($job) {
        return Carbon::parse($job->RequiredTime)->format('Y-m-d');
    });
    

    might work and return the query grouped by days for each requiredtime as per i understand it

    Login or Signup to reply.
  2. It would be helpful I guess

    $jobs = Job::where('RequiredTime', '<', Carbon::now()->addDays(10)
        ->endOfDay())
            ->where('JobCancelled', '=', 0)
            ->where('JobCompleted', '=', 0)
            ->where('Ref9', '=', null)
            ->where('CreateDateTime', '>=', Carbon::now()->startOfMonth())
            ->orderBy('RequiredTime', 'asc')
            ->get()
            ->groupBy(function ($date) {
                return Carbon::parse($date->RequiredTime)->format('Y-m-d')
            });
        
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search