使用php循环在laravel中将数据显示为交叉表

Hello there i am working with laravel 5.7 and facing an issue related to crosstab results displaying i mean that i can get result like the result image.

Please have a look at the below link then you will understand what i wanna achieve

https://webmasters.stackexchange.com/questions/124278/display-data-as-cross-tab-in-laravel

I have spent almost 7 days but could not found any proper solution please help me out i will be thankful to you

Thanks in advance.

I have join

$degreesPdfs  = DB::table('student_tbs')
    ->join('roll_no_tbs','roll_no_tbs.regno','=','student_tbs.regno')
    ->join('roll_no_com_dets','roll_no_com_dets.rollno','=','roll_no_tbs.rollno')
    ->join('college_tbs','college_tbs.college_id','student_tbs.department_id')
    ->leftjoin('subject_tbs','subject_tbs.code','=','roll_no_com_dets.subcode')
    ->leftjoin('dbo_web_part','dbo_web_part.part','=','roll_no_tbs.part')
    ->select('student_tbs.student_id','student_tbs.regno','student_tbs.stdName',
        'student_tbs.stdfName','student_tbs.department_id','student_tbs.degree_id',
        'roll_no_tbs.rollno','roll_no_com_dets.subcode','roll_no_com_dets.obt40',
        'subject_tbs.Na','subject_tbs.semester_id','subject_tbs.hours','college_tbs.name',
        'dbo_web_part.P_name','dbo_web_part.OneRTwo')
    ->where(['student_tbs.department_id' => $degAdmin_department,
        'student_tbs.degree_id' => $degree])
    ->distinct(['roll_no_com_dets.subcode','student_tbs.regno'])
    ->groupBy('dbo_web_part.OneRTwo')
    ->orderBy('roll_no_com_dets.rollno')
    ->get();