[英]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.