Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FareedR's avatar

hasManyThrough 3 table .

how can I achieve list of student in from university through campus .

db structure 

universities
id
name

campuses
id 
university_id
name

users
id
campus_id

students
id
user_id

0 likes
3 replies
staudenmeir's avatar
Level 24

Laravel has no native support for a direct relationship.

I've created a package for cases like this: https://github.com/staudenmeir/eloquent-has-many-deep

class University extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function students()
    {
        return $this->hasManyDeep(Student::class, [Campus::class, User::class]);
    }
}

// $university->students

Please or to participate in this conversation.