1

I have a table jobs. A job may have 1 or more rows in jobs_days

I want to return a list of jobs that have at least 1 'job day'. So I have this is my Jobs Model:

public function days(){
    return $this->hasMany('App\JobsDays', 'job_id', 'id');
}

Which works fine. However I need to have a list of jobs that have at least 1 job day, but sort this list by this job start date: start2(timestamp) column. I have tried using:

$jobs = Jobs::has('days')
            ->leftJoin('jobs_days', function ($join) {
                $join->where('jobs_days.job_id', '=', 'jobs.id');
            })
            ->select('jobs.id','jobs_days.start2')
            ->orderBy('jobs_days.start2', 'asc')->paginate(15);

This won't return my start2 date. If I use on in place of where, I get my start2 column, but it now returns duplicate jobs.

0

1 Answer 1

1

After many hours, I found the solution. Using on in the join, I added a simple:

->groupBy('jobs.id')

Gives me distinct job.ids. Not sure why distinct doesn't work but this seems to do the job

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.