Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

karimali1337's avatar

Laravel Many to many in 3 tables

i've 3 tables Branches,Subjects,Teachers

Branches and subjects already many to many.

i've CRUD for teacher,when i create teacher i insert info to user model to add new user with type teacher.

i want to make relation to allow me combine teachers/branches/subjects.

for example when i'm creating teacher i need to select branch/branches and subject/subjects that this teacher related to.

any hint allows me to do this ?

0 likes
3 replies
SilenceBringer's avatar
Level 55

@karimali1337 laravel do not support pivots with 3 foreign keys, so, I always recommend to make separated model with 3 keys and not think aboput it as about pivot - this way you'll be able to eager load all related data

SilenceBringer's avatar

@karimali1337 yes. make a model (TeacherScope, for example, or any other)

and create assocaited table (teach_scopes) with 3 foreign keys - user_id,branch_id,subject_id

1 like

Please or to participate in this conversation.