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.
You don't need to change your table's strtucture to use Eloquent
. All you need is to create Eloquent models and defime relationships. So, let's create Models for students
, teachers
and subjects
at first.
Student Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model {
public function subjects()
{
return $this->belongsToMany(Subject::class);
}
}
Teacher Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Teacher extends Model {
public function subjects()
{
return $this->belongsToMany(Subject::class);
}
}
Subject Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Subject extends Model {
public function students()
{
return $this->belongsToMany(Student::class);
}
public function teachers()
{
return $this->belongsToMany(Teacher::class);
}
}
Now you can use something like this:
// Get all students with related subjects
$studentsWithSubjects = \App\Student::with('subjects')->get();
// Get student with id 1 with related subjects
$student1WithSubjects = \App\Student::with('subjects')->find(1);
Use same rules with Teacher
model. Hope it helps to get you started.
Well, if you want to make a relation between Teacher
& Student
model using a many-to-many
relationship then you need to create another pivot table using id
, 'student_id' and teacher_id
and then define relationship methods in both Teacher
and Student
model so you can use relationship method from both sides, for example:
// In Student Model
public function teachers()
{
return $this->belongsToMany(Teacher::class);
}
// In Teacher Model
public function students()
{
return $this->belongsToMany(Student::class);
}
So now you'll be able to use something like this:
$teacher1WithStudents = \App\Teacher::with('students')->find(1);
$student1WithTeachers = \App\Student::with('teachers')->find(1);
I want to re-construct the question first:
There are teachers giving classes on some subjects. There are students taking classes on some subjects.
A student's teachers are the teachers giving classes on the subjects student taking.
A teacher's students are the students taking classes on the subjects teacher giving.
If that is correct I suggest you to use eager loading.
As an example, to get the student with id 1:
$student = App\Students::with(['subjects.teachers'])->find(1)->get();
Then you can do a foreach on
$student->subjects
And every subject will get teachers.
Attention! If you have just one subject for each teacher, use belongs to and has many relations.
When you get the general idea about eager loading, you can use some other query constraints on the eager loading query to get directly what you want.
Check the eager loading section here : https://laravel.com/docs/5.2/eloquent-relationships#querying-relations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.