在php / MySQL中处理数据透视表的最佳方法是什么(或者说是那种效果),我使用RFID点击来记录他们的出勤率。
我有这两个表:
Users
表
+----------------------------------+
| user_id | first_name | last_name |
| 1 | Tom | Araya |
| 2 | Kerry | King |
| 3 | Gary | Holt |
+----------------------------------+
attendance_log
表
+-------------------------------------------------------------------------+
| log_id | user_id | status | created_at | updated_at |
| 1 | 1 | entrance | 2017-08-09 13:08:57 | 2017-08-09 13:08:57 |
| 2 | 1 | entrance | 2017-08-09 13:09:01 | 2017-08-09 13:09:01 |
| 3 | 2 | entrance | 2017-08-09 13:09:15 | 2017-08-09 13:09:15 |
| 4 | 2 | entrance | 2017-08-09 13:09:18 | 2017-08-09 13:09:18 |
| 5 | 3 | entrance | 2017-08-09 13:09:30 | 2017-08-09 13:09:30 |
| 6 | 3 | entrance | 2017-08-09 13:09:33 | 2017-08-09 13:09:33 |
| 7 | 1 | entrance | 2017-08-10 13:08:57 | 2017-08-10 13:08:57 |
| 8 | 1 | entrance | 2017-08-10 13:09:01 | 2017-08-10 13:09:01 |
| 9 | 2 | entrance | 2017-08-10 13:09:15 | 2017-08-10 13:09:15 |
| 10 | 2 | entrance | 2017-08-10 13:09:18 | 2017-08-10 13:09:18 |
| 11 | 3 | entrance | 2017-08-10 13:09:30 | 2017-08-10 13:09:30 |
| 12 | 3 | entrance | 2017-08-10 13:09:33 | 2017-08-10 13:09:33 |
| 13 | 2 | entrance | 2017-08-11 13:09:30 | 2017-08-11 13:09:30 |
| 14 | 2 | entrance | 2017-08-11 13:09:33 | 2017-08-11 13:09:33 |
| 15 | 3 | entrance | 2017-08-11 13:10:45 | 2017-08-11 13:10:45 |
| 16 | 3 | entrance | 2017-08-11 13:10:45 | 2017-08-11 13:10:45 |
+-------------------------------------------------------------------------+
这是我想要实现的报告
+----------------------------------------------------+
| student id | fullname | Aug 9 | Aug 10 | Aug 11 |
| 1 | Araya, Tom | P | P | A |
| 2 | King, Kerry | P | P | P |
| 3 | Holt, Gary | P | P | P |
+----------------------------------------------------+
如果用户没有考勤记录,我该如何在雄辩中做到这一点以及如何设置为A
?因为到目前为止我的出勤报告看起来不像那样。
这是我的代码
$students = User::has('userStudentAttendance')->with(['userStudentAttendance' => function($query) use ($details,$search_from, $search_to){
$query->where('status', $details);
$query->where(function($query) use ($search_from, $search_to){
$query->whereBetween(DB::raw('Date(created_at)'), [$search_from, $search_to]);
});
$query->groupby('user_id')->groupby(DB::raw('Date(created_at)'));
}])
->leftjoin('sys_user_student', 'sys_user_student.user_id', '=', 'sys_user.user_id')
->leftjoin('sys_mf_section', 'sys_mf_section.section_id', '=', 'sys_user_student.section_id')
->leftjoin('sys_mf_grade', 'sys_mf_grade.grade_id', '=', 'sys_mf_section.grade_id')
->where('user_type_id', '4')
->where('sys_mf_section.section_id', $request->getParam('section_id'))
->where('sys_mf_grade.grade_id', $request->getParam('grade_id'))
->orderBy('sys_user.last_name')
->get();