在laravel如何计算table阀门

In laravel how to count table valve

我想统计每个中心用户注册了多少学生。

我有两个不同的 table、centerstudent

在第一个 table Center 中,我有 center_id 列。在那个 table 我有 center_id 喜欢“PP_123”.

在第二个 table student 我创建了 center_code 专栏。在那 table 我正在保存 center_code 就像“PP_123”。

如果中心用户注册学生那次 table student 包含学生数据的文件连同它存储 center_codestudent table.

中心 table 数据所有中心列表

      scenter_name                Date of join      center_code
    HALO  COMPUTERS                02-12-2019         PP_123

         scenter_name             Date of join      center_code
    SKILL EDUCATION CENTER         02-12-2019         PP_123

学生table数据所有注册学生名单

student_name student_f_name student_m_name center_code
    abc           abc           abc         PP_123

student_name student_f_name student_m_name center_code
    xyz           xyz           xyz         PP_123

student_name student_f_name student_m_name center_code
        vvv           vvv           vvv         PP_124

视图应显示如下结果

CENTER CODE       DATE OF JOIN       CENTER NAME         APPLICATION COUNT
  PP_123           02-12-2019      HALO  COMPUTERS            2
  PP_124           10-12-2019    SKILL EDUCATION CENTER       1

Controller [此代码仅显示所有中心名称和 ID]

public function center_activities()
{
    $activities_list = 
    return view('Center.cernter_activites',compact('activities_list'));
}

我的观点

                                    <table class="table table-striped table-bordered table-hover dataTables-example">
                                        <thead>
                                        <tr>
                                            <th>SL,no</th>
                                
                                         
                                            <th>CENTER CODE</th>
                                          <th>DATE OF JOIN</th>
                                            <th>CENTER NAME</th>
                                            <th> APPLICATION count</th>
                                            
                                           
                                        </tr>
                                        </thead>

                                        <tbody>
                      
                                        <tr >
                                            <td>{{ $key + 1  }}</td>
                                            <td>{{ $activities_list->center_code }}</td>
                                            <td>{{ $activities_list->center_join_date }}</td>
                                            <td>{{ $activities_list->center_name }}</td>
                                             <td>0</td>
                                            

                                        </tr>
                                       
                                  
                                        </tbody>
                      
                                    </table>

我的模型具有一对一关系

Center.php 型号

    public function student()
{
    return $this->hasOne('App\Student', 'center_code');
}

Student.php 型号

 public function center()
    {
        return $this->belongsTo('App\Center', 'center_code');
    }

假设您想要统计您正在搜索的中心的所有学生,您应该能够使用 withCount 来统计每个中心的相关记录。尝试如下设置:

中心模型:

public function students()
{
    return $this->hasMany(Student::class, 'center_code', 'center_code');
}

控制器:

$centers = Center::where('center_delete', 'NOT-DELETED')
    ->where('account_type', 'OLD_ACCOUNT')
    ->withCount('students')
    ->get();

查看:

@foreach ($centers as $center)
    ...
    <td>{{ $center->students_count }}</td>
    ...
@endforeach

Laravel 5.7 Docs - Eloquent - Relationships - Counting Related Records withCount