Home > Software design >  To query from different models and to arrange the properties in the json body
To query from different models and to arrange the properties in the json body

Time:06-18

I have an api call function below to create a summary of students based on their attendance status but I am failing to structure the JSON response body based on want I want to display because it fetching the model first

//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 ]);
                                    } 

the json body I am getting is not the one i want

{
    "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
        }
    ]
}

The json body i want is the one below

{
    "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
}

CodePudding user response:

What I think is that you select all the contents from the tables, So what to do is to iterate throughout the selected eloquent and then select what you wish to get in the Created array then return as following:

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 ]);

}

  • Related