Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在laravel中获取多对多关系计数的最佳有效方法是什么?_Laravel_Eloquent - Fatal编程技术网

在laravel中获取多对多关系计数的最佳有效方法是什么?

在laravel中获取多对多关系计数的最佳有效方法是什么?,laravel,eloquent,Laravel,Eloquent,我有多对多关系的学生和主题表(透视表是学生_主题) 学生模型 public function subjects() { return $this->belongsToMany(Subject::class, 'student_subject'); } 主题模式 public function students() { return $this->belongsToMany(Student::class, 'student_subject'); } 在这里,我要特别

我有多对多关系的
学生
主题
表(透视表是学生_主题)

学生模型

public function subjects()
{
    return $this->belongsToMany(Subject::class, 'student_subject');
}
主题模式

public function students()
{
    return $this->belongsToMany(Student::class, 'student_subject');
}
在这里,我要特别的学生科目。我试过下面的方法,效果很好,但我想用最有效的方法

一,

我通过laravel调试程序检查了SQL查询,如下所示

select * from `students` where `students`.`id` = '10' limit 1

select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
select * from `students` where `students`.`id` = '10' limit 1

select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
select * from `students` where `students`.`id` = '10' limit 1

select count(*) as aggregate from `student_subject` where `student_id` = 10
  • 我通过laravel调试程序检查了SQL查询,如下所示

    select * from `students` where `students`.`id` = '10' limit 1
    
    select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
    
    select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
    
    select * from `students` where `students`.`id` = '10' limit 1
    
    select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
    
    select * from `students` where `students`.`id` = '10' limit 1
    
    select count(*) as aggregate from `student_subject` where `student_id` = 10
    
  • 我通过laravel调试程序检查了SQL查询,如下所示

    select * from `students` where `students`.`id` = '10' limit 1
    
    select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
    
    select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
    
    select * from `students` where `students`.`id` = '10' limit 1
    
    select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
    
    select * from `students` where `students`.`id` = '10' limit 1
    
    select count(*) as aggregate from `student_subject` where `student_id` = 10
    
  • 我通过laravel调试程序检查了SQL查询,如下所示

    select * from `students` where `students`.`id` = '10' limit 1
    
    select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
    
    select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
    
    select * from `students` where `students`.`id` = '10' limit 1
    
    select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
    
    select * from `students` where `students`.`id` = '10' limit 1
    
    select count(*) as aggregate from `student_subject` where `student_id` = 10
    
    根据以上方法,哪一个是最好的,为什么?或者,如果有任何不同的最佳方法也存在?

    执行
    关系()->count()
    可能更快


    但是,如果您只需要计数,
    withCount()
    在内存消耗方面应该更好。

    谢谢您的时间,我需要一种有正确理由的最有效的方法。你的回答没有任何完美的理由。没问题!看看这是否有帮助:根据上面的链接,
    withCount
    with
    的内存效率更高。那么
    loadCount
    ?那将是急切加载与惰性加载。由于您没有根据某些动态条件加载计数,因此无需使用
    loadCount()
    运行第二个查询。因此,在您的上下文中,
    withCount()
    也是一个更好的选择。我同意您的观点。但请检查我的SQL查询中的每一点。因此,我认为第四个是最好的(花更少的时间,因为它没有检查与受试者的任何关系)