我在下面有一個 api 呼叫函式,可以根據學生的出勤狀態創建學生摘要,但是我無法根據我想要顯示的需要構建 JSON 回應正文,因為它首先獲取模型
//attendance_id = 1 it means Present if 2 it means Absent
public function getAttendanceReport($grade_id, $date){
$attendance_fetched_present = AttendanceStudent::where('created_at', 'LIKE', $date.'%')
->where('grade_id', $grade_id)
->where('attendance_id' , "1")
->addSelect([
'total_present_student' => Student::selectRaw('count(*)')
->whereIn(
'student_id',
Student::select('id')),
'total_present_boys' => Student::selectRaw('count(*)')
->whereRaw('gender = "male"')
->whereIn(
'student_id',
Student::select('id')),
'total_present_girls' => Student::selectRaw('count(*)')
->whereRaw('gender = "female"')
->whereIn(
'student_id',
Student::select('id'))
])->get();
$attendance_fetched_absent = AttendanceStudent::where('created_at', 'LIKE', $date.'%')
->where('grade_id', $grade_id)
->where('attendance_id' , "2")
->addSelect([
'total_absent_student' => Student::selectRaw('count(*)')
->whereIn(
'student_id',
Student::select('id')),
'total_absent_boys' => Student::selectRaw('count(*)')
->whereRaw('gender = "male"')
->whereIn(
'student_id',
Student::select('id')),
'total_absent_girls' => Student::selectRaw('count(*)')
->whereRaw('gender = "female"')
->whereIn(
'student_id',
Student::select('id'))
])->get();
return response()->json(['message'=>'Attendance Report in Grade',
'Present' => $attendance_fetched_present,
'Absent' => $attendance_fetched_absent ]);
}
我得到的 json 身體不是我想要的
{
"message": "Attendance Report in Grade",
"Present": [
{
"attendance_id": 1,
"student_id": 1,
"grade_id": 1,
"created_at": "2022-06-17T04:02:41.000000Z",
"updated_at": "2022-06-17T04:02:41.000000Z",
"total_present_student": 16,
"total_present_boys": 9,
"total_present_girls": 7
},
{
"attendance_id": 1,
"student_id": 2,
"grade_id": 1,
"created_at": "2022-06-17T04:02:41.000000Z",
"updated_at": "2022-06-17T04:02:41.000000Z",
"total_present_student": 16,
"total_present_boys": 9,
"total_present_girls": 7
},
],
"Absent": [
{
"attendance_id": 2,
"student_id": 16,
"grade_id": 1,
"created_at": "2022-06-17T04:17:12.000000Z",
"updated_at": "2022-06-17T04:17:12.000000Z",
"total_absent_student": 16,
"total_absent_boys": 9,
"total_absent_girls": 7
}
]
}
我想要的json主體是下面的那個
{
"message": "attendance report",
"Present":
{
"total_present_students": 10,
"total_boys_present": 5,
"total_girls_present": 5,
"created_at": "2022-06-04T14:41:34.000000Z"
},
"Absent":
{
"total_present_students": 12,
"total_boys_present": 5,
"total_girls_present": 5,
"created_at": "2022-06-04T14:41:34.000000Z"
},
"total_students_in_grade": 22
}
uj5u.com熱心網友回復:
我認為您從表中選擇了所有內容,所以要做的是遍歷選定的 eloquent,然后在 Created 陣列中選擇您希望獲得的內容,然后回傳如下:
public function getAttendanceReport($grade_id, $date){
$attendance_fetched_present = AttendanceStudent::where('created_at', 'LIKE', $date.'%')
->where('grade_id', $grade_id)
->where('attendance_id' , "1")
->addSelect([
'total_present_student' => Student::selectRaw('count(*)')
->whereIn(
'student_id',
Student::select('id')),
'total_present_boys' => Student::selectRaw('count(*)')
->whereRaw('gender = "male"')
->whereIn(
'student_id',
Student::select('id')),
'total_present_girls' => Student::selectRaw('count(*)')
->whereRaw('gender = "female"')
->whereIn(
'student_id',
Student::select('id'))
])->get();
$attendance_fetched_absent = AttendanceStudent::where('created_at', 'LIKE', $date.'%')
->where('grade_id', $grade_id)
->where('attendance_id' , "2")
->addSelect([
'total_absent_student' => Student::selectRaw('count(*)')
->whereIn(
'student_id',
Student::select('id')),
'total_absent_boys' => Student::selectRaw('count(*)')
->whereRaw('gender = "male"')
->whereIn(
'student_id',
Student::select('id')),
'total_absent_girls' => Student::selectRaw('count(*)')
->whereRaw('gender = "female"')
->whereIn(
'student_id',
Student::select('id'))
])->get();
$present_student_arr = [];
$absent_student_arr = [];
foreach ($attendance_fetched_present as $present) {
$present_student_arr[] = array(
'total_present_students' => $present['total_present_student'],
'total_boys_present' => $present['total_present_boys'],
'total_girls_present' => $present['total_present_girls'],
'created_at' => $present['created_at'],
);
}
foreach ($attendance_fetched_absent as $absent) {
$absent_student_arr[] = array(
'total_absent_students' => $absent['total_absent_student'],
'total_boys_absent' => $absent['total_absent_boys'],
'total_girls_absent' => $absent['total_absent_girls'],
'created_at' => $present['created_at'],
);
}
return response()->json(['message'=>'Attendance Report in Grade',
'Present' => $present_student_arr,
'Absent' => $absent_student_arr ]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493176.html
上一篇:將暮光之城資料匯入谷歌表格
下一篇:將API請求附加到另一個字典
