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 ?
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With