I have three table:
Teacher have one to many relation with ClassRoom
Student have many to many relation with ClassRoom
how can i retrieve all Students of a Teacher using Eloquent methods without using foreach?
$teacher = Teacher::with('classrooms.students')->find($someId); //eager load $studentsArray = $teacher->classrooms->pluck('students'); //array of students with duplicates $students = (new Collection($studentsArray))->collapse()->unique(); //collection of unique students