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.

  1. scheduledprograms which has many
  2. scheduledprogramsegments which belongs many
  3. 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

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -