1
votes

Is it possible with eloquent to query a chain of linked tables with some tables having more than one relationships and get all the information associated?

For example:

  • Table Schools has a one to many relationship with Courses
  • Table Courses has a many to many relationship with Students
  • Table Teachers has a one to many relationship with Courses
  • Table Supervisors has a one to many relationship with Students
  • Table Books has a many to many relationship with Students

if one wanted all the information for a particular school (i.e. students, courses, teachers, books, supervisors that are linked to the school) would that be possible with eloquent? Or is it only possible with raw SQL?

I'm not looking for the solution of the example, just the approach for querying for multiple tables where each has multiple relationships.

1

1 Answers

1
votes

Yes. This can be acheived with eloquent with Nested eager loading

$school=School::with('cources','cources.students.supervisor','cources.teacher','...')->first();
foreach($school->cources as $cource)
{
   echo $cource;
   echo $cource->teacher;
   foreach($cource->students as $student)
   {
      echo $student;
      echo $student->supervisor;
   }
...