我的表格(和 Laravel 模型关系):
projects -> tasks -> commands
计算任务数量:
$body = Project->withCount('tasks')->get();
美好的。
然后我想计算所有任务的命令数。 SQL 等效代码为:
select
count(commands.*)
from projects
inner join tasks on tasks.project_id = projects.id
inner join commands on commands.task_id = tasks.id
如何通过 Eloquent 获取?由于某些原因,查询生成器不可接受。
不幸的是,witchCount
它不够灵活with
,我不能使用withCount('tasks.commands')
您可以将
Task
关系添加到Project
模型中,也可以Command
将关系添加到模型中。Task
之后,您可以使用 查询两者
withCount
。