October 22, 2024
Chicago 12, Melborne City, USA
PHP

Laravel 11.x Eloquent Way To Get Relation Through Relations (4 Total Tables)


I’m using latest version of Laravel. I’m trying to setup a model method relationship that passes through other intermediary tables.

Understanding the table structure below, my goal is retrieve a collection of Students (only) which are enrolled in a course that belongs to a curriculum model.

Curriculum->Course->Students Course->Student

I’ve come close, as you can see in the Curriculum model, student() method, I can retrieve a collection of all courses along with their students… but I don’t want the courses along with it.

Is there an Eloquent way to accomplish this?

Thank you!

Table Structure

curriculum
id
..

courses
id
curriculum_id
..

students_courses
id
course_id
student_id
..

students
id
..

Models

curriculum

    /**
     * @return HasMany
     */
    public function course(): HasMany
    {
        return $this->hasMany(Course::class);
    }

    /**
     * @return HasManyThrough
     */
    public function class(): HasManyThrough
    {
        return $this->hasManyThrough(ClassModel::class, Course::class);
    }

    /**
     * @return HasManyThrough
     */
    public function student()
    {
        // this works as expected, but brings courses too, I only want students.
        return $this->hasManyThrough(StudentCourse::class, Course::class)->with('student');
    }

course

    /**
     * @return HasMany
     */
    public function class(): HasMany
    {
        return $this->hasMany(ClassModel::class);
    }

    /**
     * @return BelongsTo
     */
    public function curriculum(): BelongsTo
    {
        return $this->belongsTo(Curriculum::class, 'curriculum_id', 'id');
    }

    /**
     * @return HasManyThrough
     */
    public function student(): HasManyThrough
    {
        return $this->hasManyThrough(Student::class, StudentCourse::class,
            'course_id',
            'id',
            'id',
            'student_id'
        );
    }

students_courses


    /**
     * @return BelongsTo
     */
    public function student(): BelongsTo
    {
        return $this->BelongsTo(Student::class);
    }

    /**
     * @return BelongsTo
     */
    public function course(): BelongsTo
    {
        return $this->BelongsTo(Course::class);
    }

student


    /**
     * @return HasOne
     */
    public function user(): HasOne
    {
        return $this->HasOne(User::class, 'userID', 'user_id');
    }

    /**
     * @return HasManyThrough
     */
    public function course(): HasManyThrough
    {
        return $this->HasManyThrough(Course::class, StudentCourse::class,
            'student_id',
            'id',
            'id',
            'course_id'
        );
    }

    /**
     * @return HasManyThrough
     */
    public function class(): HasManyThrough
    {
        return $this->HasManyThrough(ClassModel::class , StudentClass::class,
        'student_id',
        'id',
        'id',
        'class_id'
        );
    }



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video