Jun 19, 2019
2
Level 1
Help me understand the problem with this hasOneThrough relationship!
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 or to participate in this conversation.