Php 在laravel中如何计算表阀

Php 在laravel中如何计算表阀,php,laravel,laravel-5,laravel-5.7,Php,Laravel,Laravel 5,Laravel 5.7,我想统计每个中心的用户注册的马赫数 我有两个不同的表格,center和student 在第一个表Center中,我有center\u id列。在那个表中,我有一个中心id,如“PP_123” 在第二个表studentI中创建了center\u code列。在该表中,我保存了中心代码,如“PP_123” 如果中心用户注册学生,则第二个时间表student包含学生数据的文件将center\u code存储在student表中 中心表数据所有中心列表 scenter_name

我想统计每个中心的用户注册的马赫数

我有两个不同的表格,
center
student

在第一个表
Center
中,我有c
enter\u id
列。在那个表中,我有一个中心id,如“PP_123”

在第二个表
student
I中创建了
center\u code
列。在该表中,我保存了中心代码,如“PP_123”

如果中心用户注册学生,则第二个时间表
student
包含学生数据的文件将
center\u code
存储在
student
表中

中心表数据所有中心列表

      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
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
学生表数据所有注册学生列表

      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
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
                                    <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>
控制器[此代码仅显示所有中心的名称和id]

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

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
                                    <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>
Student.php模型

    public function student()
{
    return $this->hasOne('App\Student', 'center_code');
}
 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作为$center)
...
{{$center->students\u count}
...
@endforeach

withCount

您在这些模型之间设置了关系吗?@lagbox question updated仍然不确定您要计算什么,因为这是一对一关系,这些表中是否有一个字段包含计数?如果有一对多,我可以看出需要做一个count@lagbox问题已更新,因此关系应为一对多。。学生属于中心,中心有许多学生,但基于中心代码不是中心的“id”?