sql - how to get max count of records in a belongs to many relation with a where clause -
i have 3 models representing programs , program segments attendees can register for.
- scheduledprograms which has many
- scheduledprogramsegments which belongs many
- attendees (which belongs many scheduledprogramsegments)
attendees has field registered bool 1/0 indicate if attendee registered or not.
i want query finds maximum attendee registration count in programsegments program. i.e.
max( scheduledprogram::find(id)->scheduledprogramsegments->with([('attendees') => function($query) { $query->where('registered'); }])->count(); );
is there way write query in eloquent or query builder? using raw expressions query builder if way?
edit: clarify, have pivot table field registered
, eager load count of records, marked registered
.
so far seems not possible unless add , maintain count field don't want do, run running separate query count each individual record retrieve pivot table.
try update part in query
$query->where('registered')->max('column_name');
Comments
Post a Comment