Trouble joining 3 different tables in Laravel

  laravel, mysql, php

I am using Laravel 7 and am trying to join 3 tables and for the most part, it is working great. The problem is that my table is returning the first test result it sees which is 33% and repeating it for all the other rows. I am new to joining in Laravel and need some assistance.
Here are my tables
oex_students
oex_students table
oex_results table
oex_results table
and finally the oex_exam_masters table
oex_exam_masters table
In the dashboard, I am calling the join in the index function within the AdminController.php

public function index()
    {
      $data['portal'] = Oex_portal::get()->toArray();

      $data['exams'] = Oex_exam_master::where('status', '1')->get()->toArray();
      $data['result_info'] = Oex_result::select('oex_results.*', 'oex_results.result as result')->get()->first();
      $data['students'] = Oex_students::select(['oex_students.*', 'oex_exam_masters.title as exam_name'])
      ->join('oex_results', 'oex_students.id', '=', 'oex_results.user_id')
      ->join('oex_exam_masters', 'oex_results.exam_id', '=', 'oex_exam_masters.id')
      ->get()
      ->toArray();


      $data['category'] = Oex_category::where('status', '1')->get()->toArray();
      $data['exams'] = Oex_exam_master::select(['oex_exam_masters.*', 'oex_categories.name as cat_name'])->join('oex_categories', 'oex_exam_masters.category', '=', 'oex_categories.id')->get()->toArray();
      return view('admin.dashboard', $data);
  }

In my dashboard where I am displaying the table is:

 @foreach($students as $key => $student)
      <tr>
         <td>{{ $key+1 }}</td>
          <td>{{ $student['name'] }}</td>
          <td>{{ $student['email'] }}</td>
          <td>{{ $student['mobile_no'] }}</td>
           <td>{{ $student['exam_name'] }}</td>
           {{-- <td>{{ $student['result'] }}%</td> --}}
           <td>{{ $result_info->result }} %</td>
           {{-- <td>N/A</td> --}}


           @if($student['status']== 1)
           <td><input data-id="{{ $student['id'] }}" class="student_status" type="checkbox" name="status" checked></td>
           @else
           <td><input data-id="{{ $student['id'] }}" class="student_status" type="checkbox" name="status"></td>
           @endif

       </tr>
   @endforeach

Here is an image where the information is displayed:
table view
I want to call the result where the oex_students.id = oex_results.user_id but I am not sure how to go about this using Laravel. Thank you very much in advance for your help.

Source: Laravel

Leave a Reply