【问题标题】:In laravel how to count table valve在laravel中如何计算表阀
【发布时间】:2019-12-15 03:41:39
【问题描述】:

我想统计每个中心用户他们注册的学生人数。

我有两个不同的表,centerstudent

在第一个表 Center 中,我有 center_id 列。在该表中,我有 center_id,例如“PP_123”。

在第二张表student 中创建了center_code 列。在该表中,我将 center_code 保存为“PP_123”。

如果中心用户注册学生该时间第二个表student 文件与学生数据一起存储center_codestudent 表中。

中心表数据 ALL CENTERS LIST

      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

学生表数据 ALL REGISTERED STUDENT LIST

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

控制器 [此代码仅显示所有中心名称和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');
    }

【问题讨论】:

  • 你有这些模型之间的关系设置吗?
  • @lagbox 问题已更新
  • 仍然不确定您要计算什么,因为这是一对一的关系,这些表中是否有一个字段可以计算?如果有一对多,我可以看到需要进行计数
  • @lagbox 问题已更新
  • 所以关系应该是一对多..学生属于中心,中心有很多学生,但基于center_code而不是中心的'id'?

标签: php laravel laravel-5 laravel-5.7


【解决方案1】:

假设您想要统计您正在搜索的中心的所有学生,您应该可以使用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 RecordswithCount

【讨论】:

    猜你喜欢
    • 2012-05-11
    • 2020-01-22
    • 2019-01-18
    • 2021-05-15
    • 2016-06-14
    • 2019-02-23
    • 1970-01-01
    • 2019-11-03
    • 2021-06-13
    相关资源
    最近更新 更多