I have the following models/tables:
branch:
id - integer
department:
id - integer
teacher:
id - integer
branch_department_id - integer
branch_department:
id - integer
branch_id - integer
department_id - integer
branch
has many departments
through the pivot table branch_department
branch_department
has many teacher
sI want to model a hasManythrough
relationship between branch
and teacher
s
so from a specific branch
i want to get all teachers through every branch_department
that is related to that branch
How can I define this?
If you haven't define a BranchDepartment
model.
class BranchDepartment extends Pivot
{
}
From here you can just define the hasManyThrough
relationship on your Branch
model.
public function teachers()
{
return $this->hasManyThrough(
Teacher::class,
BranchDepartment::class,
secondKey: 'branch_department_id',
);
}