mm511
3 months ago

Help me understand the problem with this hasOneThrough relationship!

Posted 3 months ago by mm511

I am building a simple application status tracker to be used by counselors.

The relevant models here are as follows:

schools
  -id

students
  -id
  -school_id

applications
  -id
  -student_id

Note: 'School' in this example is the high school from which the student graduates, not the college to which they apply.

I have the following relationships:

class School

    public function students()
    {
        return $this->hasMany('App\Student');
    }

    public function applications()
    {
        return $this->hasManyThrough('App\Application', 'App\Student');
    }


class Student

public function school()
    {
        return $this->belongsTo('App\School');
    }

    public function applications()
    {
        return $this->hasMany('App\Application');
    }


class Application

    public function student()
    {
        return $this->belongsTo('App\Student');
    }

All of this works fine, including the hasManyThrough relationship on the School model.

What I cannot get to work is something like this, on the Application model:


public function school()
{
    return $this->hasOneThrough('App\School', 'App\Student');
}

And I do not understand why. I can do a simple chain, something like $this->student->school, and that works... but the hasOneThrough doesn't.

Any thoughts?

Please sign in or create an account to participate in this conversation.