这是一个架构:
users: id
schools: id | district_id
school_users: id | school_id | user_id
districts: id
如何在Eloquent中建立关系以返回$ district->用户?
class District
{
public function users()
{
//something that mirrors this functionality
return $this->hasManyThroughThrough("User","SchoolUser","School");
}
}
答案 0 :(得分:0)
你应该跨越两种关系:
class District
{
public function users()
{
return $this->hasMany('School');
}
}
class School
{
public function users()
{
return $this->belongsToMany('User');
}
}
class User
{
}
$books = District::with('schools.users')->get();