Skip to content

Instantly share code, notes, and snippets.

@bran921007
Created January 5, 2022 02:59
Show Gist options
  • Select an option

  • Save bran921007/c71b6fc18e679a63e8f7bd34da94aaae to your computer and use it in GitHub Desktop.

Select an option

Save bran921007/c71b6fc18e679a63e8f7bd34da94aaae to your computer and use it in GitHub Desktop.
add conditional aggregate in mysql
Course::query()
->select('courses.*')
->addSelect([
"lessons count' => Lesson::query ()
->select (DB: : raw( 'COUNT (lessons. id) '))
->whereColumn ('lessons.course id','courses. id'),
'total_length' => Lesson::query()
->select (DB: : raw ('SUM (lessons. video_length) '))
->whereColumn('lessons. course_id', 'courses.id') ,
])
->orderBy('title")
->paginate(20)
->withQueryString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment