简体   繁体   English

如何通过 Eloquent 获取单个集合中每位教师的学生数

[英]How to get the counts of Student for each Teachers in a single collection by Eloquent

A Teacher has many Students.一个老师有很多学生。 When I am showing the Teachers list I also want to show the counts of Student for each Teachers.当我显示教师列表时,我还想显示每位教师的学生人数。 How can I do this by using Eloquent?我如何使用 Eloquent 来做到这一点?

I can find the Teachers from this, $teacher= Teacher::where('teacher_status','active')->get();我可以从这里找到教师, $teacher= Teacher::where('teacher_status','active')->get();

I can find the Student count from this $student_count = Student::where('teacher_id','teachers.id')->count();我可以从这个$student_count = Student::where('teacher_id','teachers.id')->count();中找到学生人数

How can I conbine this two query and return the response in a single array/collection?我如何结合这两个查询并在单个数组/集合中返回响应?

If you want to count the number of students related to teacher without actually loading them you may use the withCount method and this add new propery named by a {relation}_count column on your resulting models.如果你想计算与教师相关的学生人数而不实际加载他们,你可以使用 withCount 方法,这会在你的结果模型上添加一个由 {relation}_count 列命名的新属性。 For example:例如:

Teacher::where('teacher_status','active')->withCount('students')->get();

Also you need and to your Teacher model hasMany relation method to Students你还需要和你的老师 model hasMany relation method to Students

In your teacher model, create the relationship students:在你的老师 model 中,创建学生关系:

class Teacher extends Model
{
    public function students()
    {
        return $this->hasMany(Student::class, 'teacher_id');
    }
}

In your controller you can do the following:在您的 controller 中,您可以执行以下操作:

public function example(){
    $teachers = Teacher::where('teacher_status','active')->withCount('students')->get();
    
    return view('teacherViewExample', compact('teachers'));
}

In your view (teacherViewExample):在你看来(teacherViewExample):

<table>
  <thead>
    <tr>
      <th>Teacher Name</th>
      <th>Students Count</th>
    </tr>
  </thead>
  <tbody>
    @foreach($teachers as $teacher)
    <tr>
      <td>{{ $teacher->name }}</td>
      <td>{{ $teacher->students_count }}</td>
    </tr>
    @endforeach
  </tbody>
</table>

Full documentation on how to use withCount() here: https://laravel.com/docs/9.x/eloquent-relationships#counting-related-models有关如何在此处使用withCount()的完整文档: https://laravel.com/docs/9.x/eloquent-relationships#counting-related-models

Sometimes you may want to count the number of related models for a given relationship without actually loading the models.有时您可能想要计算给定关系的相关模型的数量,而无需实际加载模型。 To accomplish this, you may use the withCount method.为此,您可以使用 withCount 方法。 The withCount method will place a {relation}_count attribute on the resulting models: withCount 方法将在生成的模型上放置一个 {relation}_count 属性:

In case frontend and backend are separated, ie Laravel for backend, Angular for frontend, below are examples for Laravel:如果前后端分离,即后端为Laravel,前端为Angular,下面以Laravel为例:

In api.php,在 api.php,

Route::get('teacher-with-students-count', 'TeacherController@getTeacherWithStudentsCount');
 

In Teacher.php (model)在老师.php(模型)

class Teacher extends Model
{
    public function students()
    {
        return $this->hasMany(Student::class, 'teacher_id', 'id');
    }
}

In TeacherController.php在 TeacherController.php

public function getTeacherWithStudentsCount()
    {
        $teachers = Teacher::where('teacher_status','active')->withCount('students')->get();

        return $this->giveSuccessResponse($teachers);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取不在 Laravel Eloquent 中的集合中的项目? - How to get items are not in a collection in Laravel Eloquent? Laravel Eloquent - 如何使用 Eloquent 只获得每个父母的 2 个孩子? - Laravel Eloquent - how to get only 2 child of each parent using Eloquent? 如何在 Laravel Eloquent Collection 的 each 方法中使用 break 或 continue? - How to use break or continue with Laravel Eloquent Collection's each method? 如果集合有数据或计数 &gt; 0,如何获得 Eloquent 集合? - How to get Eloquent collection if collection has data or count is > 0? Laravel如何添加选择单个表每行的所有总计数 - Laravel how to addSelect all total counts on a single table each row 使用Eloquent获取集合后,我可以更改该集合中单个对象的位置吗? - After using Eloquent to get a collection, can I change the location of a single object in that collection? 如何对这个雄辩的收藏进行排序? - How to sort this eloquent collection? 如何从雄辩的收藏中获取GroupBy计数? - How do I get GroupBy count from an eloquent collection? 如何按条件从 laravel eloquent 集合中获取项目? - How to get item from laravel eloquent collection by conditions? 如何从 Eloquent 模型的集合中删除不必要的字段(关系)? - How to get rid of unnecessary field (relation) from collection of Eloquent models?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM