alex10791
alex10791

Reputation: 454

Eloquent, multiple relationships per table

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:

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.

Upvotes: 1

Views: 242

Answers (1)

Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

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;
   }
...

Upvotes: 1

Related Questions