我有以下模型/表:
branch:
id - integer
department:
id - integer
teacher:
id - integer
branch_department_id - integer
branch_department:
id - integer
branch_id - integer
department_id - integer
branch
通过数据透视表有很多departments
branch_department
branch_department
有很多teacher
s我想模拟
hasManythrough
和branch
s之间的
teacher
关系
所以从一个特定的
branch
我想让所有老师通过与那个相关的每一个branch_department
branch
我怎么定义这个?
添加 branch_department 模型和:
class Branch extends Model
{
public function teachers()
{
return $this->hasManyThrough(
Teacher::class,
BranchDepartment::class,
'branch_id',
'id',
'id',
'teacher_id'
);
}
}
如果你还没有定义一个
BranchDepartment
模型。
class BranchDepartment extends Pivot
{
}
从这里你可以在你的
hasManyThrough
模型上定义Branch
关系。
public function teachers()
{
return $this->hasManyThrough(Teacher::class, BranchDepartment::class);
}