Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex join in laravel

I try to do the following complex join to get all subjects that started according to the student's division

$studentID  = Student::find($id);

$divisionID = Student::where('id', $studentID->id)->select('division_id')->pluck('division_id');
$payments = Payment::all();
$discounts = Discount::all();

// This the Big join to get all subjects that started according to the student's division
  $subjectStart = SubjectStart::join('teacher_subject' ,'teacher_subject.id' ,'=','subject_start.teach_sub_id')
                              ->join('subject' ,'subject.id' ,'=' , 'teacher_subject.subject_id')
                              ->join('subject_division','subject_division.subject_id' ,'=' ,'subject.id')
                              ->join('division' ,'division.id','=','subject_division.division_id')
                              ->where('division.id' ,$divisionID)
                              ->where('lecture_num' ,'>' , 0)
                              ->get();

After that i tried to loop over $subjectStart as the following

@foreach($subjectStart as $st)
   {{$st->id}}
   {{$st->start_date}}
   {{$st->lecture_num}}
   .............
 @endforeach

The result of join displays correctly except $st->id , displays as division_id not subject_start_id , for example if the division _id = 5, the result of loop over $subjectStart , $st->id will be 5,5,5,.....

Any Suggestions ?

like image 921
Muhammad Atallah Avatar asked May 05 '15 04:05

Muhammad Atallah


2 Answers

I solved it just use select statement as the following

// This the Big join to get all subjects that started according to the student's division
  $subjectStart = SubjectStart::join('teacher_subject' ,'teacher_subject.id' ,'=','subject_start.teach_sub_id')
                             ->join('subject' ,'subject.id' ,'=' , 'teacher_subject.subject_id')
                             ->join('subject_division','subject_division.subject_id' ,'=' ,'subject.id')
                             ->join('division' ,'division.id','=','subject_division.division_id')
                             ->where('division.id' ,$divisionID)
                             ->where('lecture_num' ,'>' , 0)
                             ->select('subject_start.id', 'subject_start.teach_sub_id', 'subject_start.group_id','subject_start.lecture_num','subject_start.start_date')
                             ->get();
like image 155
Muhammad Atallah Avatar answered Sep 23 '22 18:09

Muhammad Atallah


You just have to override your above code with this one:

  $subjectStart = SubjectStart::selectRaw('subject.start_id AS id, *')
                          ->join('teacher_subject' ,'teacher_subject.id' ,'=','subject_start.teach_sub_id')
                          ->join('subject' ,'subject.id' ,'=' , 'teacher_subject.subject_id')
                          ->join('subject_division','subject_division.subject_id' ,'=' ,'subject.id')
                          ->join('division' ,'division.id','=','subject_division.division_id')
                          ->where('division.id' ,$divisionID)
                          ->where('lecture_num' ,'>' , 0)
                          ->get();
like image 43
Bagaskara Wisnu Gunawan Avatar answered Sep 22 '22 18:09

Bagaskara Wisnu Gunawan