通过 laravel eloquent 查询四级表中的数据

双神

我有这个 SQL 结构来查询学校和男性学生的数量,我正在寻求帮助以将其转换为 laravel eloquent

SELECT *
FROM schools && count(students has(gender == 'male'))
JOIN grades ON (grades.schools = schools.school_id)
JOIN streams ON (stream.schools = schools.school_id)
JOIN students ON (student.schools = schools.school_id)

这就是我在模式学校模式中所做的

 Schema::create('schools', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('educationLevel');
            $table->foreignId('ward_id')
            ->constrained('wards')
            ->onUpdate('cascade');
            $table->timestamps();
        });

年级

Schema::create('grades', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->foreignId('school_id')->constrained('schools')
            ->onDelete('cascade');
    $table->timestamps();});

溪流

 Schema::create('streams', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->foreignId('grade_id')
            ->constrained('grades')
            ->onDelete('cascade');
            $table->timestamps();
    });

学生

Schema::create('students', function (Blueprint $table) {
           $table->id();
            $table->string('student_name');
            $table->string('gender');
            $table->foreignId('stream_id')
            ->constrained('streams')
            ->onDelete('cascade');
            $table->timestamps();
        });

这是我之前在学校控制器中尝试过的

$schools = School::select(['name'])->withCount('students')->where('students', function($query){
    $query->where('gender', 'male');
})
->get();

在学校模型中,我在下面做了这个

 public function grades()
    {
        return $this->hasMany(Grade::class);
    }

  
    public function students(){
        return $this->hasManyThrough(Student::class, Stream::class, Grade::class);
    }

该模型的关系是一对多的,如下面的学校->有->年级->有->流->学生(性别=男性或女性)

唐卡纳什

您可以利用addSelect来获得所需的输出

School::query()
  ->addSelect([
      /** Total no of students in school */
      'count_students' => Student::selectRaw('count(*)')
          ->whereIn(
            'stream_id', 
            Stream::select('id')->whereIn(
              'grade_id',
              Grade::select('id')->whereColumn('school_id', 'schools.id')
            )
          ),
      /** Total no of "gender = male" students in school */
      'count_male' => Student::selectRaw('count(*)')
          ->whereRaw('gender = "male"')
          ->whereIn(
            'stream_id', 
            Stream::select('id')->whereIn(
              'grade_id',
              Grade::select('id')->whereColumn('school_id', 'schools.id')
            )
          ),
      /** Total no of "gender = female" students in school */
      'count_female' => Student::selectRaw('count(*)')
          ->whereRaw('gender = "female"')
          ->whereIn(
            'stream_id', 
            Stream::select('id')->whereIn(
              'grade_id',
              Grade::select('id')->whereColumn('school_id', 'schools.id')
            )
          ),
      /** Total no of "gender = other" students in school */
      'count_other' => Student::selectRaw('count(*)')
          ->whereRaw('gender = "other"')
          ->whereIn(
            'stream_id', 
            Stream::select('id')->whereIn(
              'grade_id',
              Grade::select('id')->whereColumn('school_id', 'schools.id')
            )
          )
  ])->get();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过 Laravel Eloquent 中的 json_encode 变量查询

Laravel Eloquent通过ID删除

通过 Laravel / eloquent 上的枢轴数据过滤结果

Laravel Eloquent-查询数据透视表

如何在 Laravel eloquent 中查询数据透视表的计数

如何通过数据透视表上的关系计数进行排序-Laravel 4 / Eloquent

Laravel eloquent 构建查询

Laravel Eloquent 查询与关系

查询 Eloquent laravel

在 Laravel Eloquent 中如何通过辅助表定义关系?(总是返回 0 关系)

Laravel Eloquent - 数据透视表

Laravel Eloquent从多个表中检索数据

Laravel 5-如何通过查询生成器或Eloquent在两个以上的表/查询中使用联合?

如何在没有 foreach 的情况下通过 3 加入 Laravel Eloquent 模型进行查询

如何通过一对多关系对 Laravel eloquent 查询进行排序

通过 id 获取记录并使用 Laravel 和 eloquent 查询与“wherebetween”的关系

Laravel Eloquent-通过连接选择列文本,例如来自其他表的列文本

Laravel Eloquent-通过传递JSON变量插入数据库

为什么通过Laravel Eloquent检索的数据具有这种行为?

如何在laravel查询构建器或eloquent中使用两个表通过created_at列计算行数?

如何在Laravel Eloquent中通过急于加载的键索引返回的数组?

复杂的 SQL / Laravel Eloquent 查询

制定趋势查询-Laravel Eloquent

Laravel Eloquent左联接查询

MySQL查询到Laravel Eloquent

Laravel Eloquent 代替查询构建

Laravel 8 eloquent 查询关系

原始查询到 Laravel Eloquent

在Laravel中用Eloquent查询联接表