I think you can still use contraints with withSum.
Jul 18, 2021
10
Level 63
How to use withSum() with a condition where() ?
Hello,
Here is a query.
$teachingUnitSubjects = TeachingUnitSubject::
with(['subject' => function($query) {
return $query->select('id', 'name');
}])
->withSum('courses', 'hours_number')
->where('teaching_unit_id', $teachingUnitId)->get();
I'd like to add a where() closure on the withSum. How may I do it ?
Thanks for your help.
Vincent
Level 56
->withSum([
'courses' => fn ($query) => $query->where('column', 'value'),
'hours_number'
])
3 likes
Please or to participate in this conversation.