I have 5 tables students
,teachers
,subjects
,student_subject
,subject_teacher
student have many to many relationship with subject similarly teacher have many to many relation with subject .
students table
id | name | email
teachers table
id | name | email
subjects table
id | name
subject_teacher table:
id | teacher_id | subject_id
student_subject table:
id | student_id | subject_id
above is my database structure In models of Student,Teacher,Subject
i define many to many relation.
So if i want to get subject of teacher i simply do Teacher::find(1)->subjects()->get()
. In my current database structure i have not direct relation between students and teachers but i want to get all students of teacher i can do this with query like
Student::join('student_subject', 'students.id', '=', 'student_subject.student_id')
->join('subject_teacher', 'student_subject.subject_id', '=', 'subject_teacher.subject_id')
->where('subject_teacher.teacher_id', '=',1)
->groupBy('students.name');
My problem i don't want query i want to do this with eloquent what i do what i will change in my relationship please help me
Note: I don't want to change in my database structure. I know if it is possible with query then it will surely possible with eloquent.