我想创建一个与此等价的查询
select classroom.*, count(students.id) as headcount
from classroom
left join students on classroom.id = students.classroom_id
where classroom.id = 1
但是经过一些试验,我得到的想法是我无法在同一查询中混合使用“选择”和“计数”。我一定是错的...但是我无法弄清楚。我也没有看到如何使用雄辩的ORM。
类似的东西:
$classroomDescription= DB::table('classroom')
->leftjoin('students ', 'classroom.id', '=', 'students .id')
->select('classroom.*')
->count('students.id as headcount')
->where('classroom.id','=', $id)
->toSql();
//->get();
除非我要运行两个单独的查询并合并结果……但这听起来效率低下。