【问题标题】:Laravel 4 tables involved涉及 Laravel 4 个表
【发布时间】:2022-11-22 11:43:55
【问题描述】:

我有 4 个表:类别、课程、科目、学生。我只想获得该类别的学生人数。

类别

  • 编号
  • 姓名

培训班

  • 编号
  • 类别编号
  • 课程

科目

  • 编号
  • 课程编号
  • 主题

学生

  • 编号
  • 主题编号
  • 姓名

例如,我希望结果像 类别 || # 注册学生 科学|| 862

是否可以在这上面使用 hasManyThrough?我得不到我想要的结果。

【问题讨论】:

  • 你是如何建立关系的?

标签: laravel


【解决方案1】:
Category::select("categories.id as category_id", "categories.name as category_name")
->selectRaw('COUNT(students.id) as enrolled_students')
->leftJoin("courses", "categories.id", "=", "courses.category_id")
->leftJoin("subjects", "courses.id", "=", "subjects.course_id")
->leftJoin("students", "subjects.id", "=", "students.subject_id")
->groupBy("categories.id", "categories.name")
->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-07
    • 2015-06-28
    • 2015-05-12
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多