0

I want to display what kind of data I have described below, but I don't know the keywords.

For now, this is my data:

enter image description here

And this is the result I want:

enter image description here

This is, my controller:

$data = DB::table('tr_doctor_schedules as sch')
    ->select('sch.id as schedule_id','doctor_name','hospital_name','estimated_practice_time','day','from_time','until_time')
    ->join('mst_hospitals as hos', 'hos.id', '=', 'sch.hospital_id')
    ->join('mst_doctors as doc', 'doc.id', '=', 'sch.doctor_id')
    ->join('tr_doctor_schedule_details as dtl', 'dtl.doctor_schedule_id', '=', 'sch.id')
    ->where('sch.hospital_id', $hosId)
    ->where('sch.doctor_id', $docId)
    ->first();

if ($data) {
    return response()->json([
        'success' => true,
        'message' =>'success',
        'data'    => $data
    ], 200);
} else {
    return response()->json([
        'success' => false,
        'message' => 'Data not found.',
    ], 400);
}

Thanks

3
  • 1
    It seems like you need to use group_concat for day column Commented Aug 31, 2020 at 5:19
  • why the from time is 8 not 9, and until time is 18 not 19 , is there a logic for it? Commented Aug 31, 2020 at 5:29
  • from line 9 to the end, no logic there, but I just added the response too Commented Aug 31, 2020 at 6:47

1 Answer 1

1

Try this

   $data = DB::table('tr_doctor_schedules as sch')
        ->selectRaw('sch.id as schedule_id,doctor_name,hospital_name,estimated_practice_time,GROUP_CONCAT(day) as day,from_time,until_time')
        ->join('mst_hospitals as hos', 'hos.id', '=', 'sch.hospital_id')
        ->join('mst_doctors as doc', 'doc.id', '=', 'sch.doctor_id')
        ->join('tr_doctor_schedule_details as dtl', 'dtl.doctor_schedule_id', '=', 'sch.id')
        ->where('sch.hospital_id', $hosId)
        ->where('sch.doctor_id', $docId)
        ->groupBy('sch.id')
        ->first();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot, its solved. In postgres i used array_agg()
yeah .. my pleasure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.