I have 3 models representing Programs and Program Segments that Attendees can register for.
我有3个模型代表参加者可以注册的程序和程序段。
- ScheduledPrograms which has many
- ScheduledProgramSegments which belongs to many
- Attendees (which belongs to many ScheduledProgramSegments)
ScheduledPrograms有很多
ScheduledProgramSegments属于很多
与会者(属于许多ScheduledProgramSegments)
Attendees has a field registered that is a bool 1/0 to indicate if an attendee is registered or not.
与会者有一个注册的字段是bool 1/0,用于指示与会者是否已注册。
I want to do a query that finds the maximum attendee registration count in a ProgramSegments for a Program. I.E. Something like
我想进行查询,以查找程序的ProgramSegments中的最大与会者注册计数。 I.E.就像是
Max( ScheduledProgram::find(id)->ScheduledProgramSegments->with([('attendees') => function($query)
{
$query->where('registered');
}])->count();
);
Is there a way to write this query in Eloquent or Query Builder? Even using the Raw Expressions from Query Builder if that is the only way?
有没有办法在Eloquent或Query Builder中编写此查询?如果这是唯一的方法,即使使用查询生成器的原始表达式?
Edit: To clarify, I have a pivot table with a field registered
and I would like to eager load the count of records, marked registered
.
编辑:为了澄清,我有一个注册字段的数据透视表,我想急切加载记录的计数,标记已注册。
So far it seems that this is not possible unless I add and maintain a count field which I don't want to do, so I run am running a separate query to get the count for each individual record I retrieve from the pivot table.
到目前为止,似乎这是不可能的,除非我添加并维护一个我不想做的计数字段,所以我运行一个单独的查询来获取我从数据透视表中检索的每个单独记录的计数。
1 个解决方案
#1
1
try to update this part in your query
尝试在查询中更新此部分
$query->where('registered')->max('column_name');
#1
1
try to update this part in your query
尝试在查询中更新此部分
$query->where('registered')->max('column_name');