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

dmhall0's avatar

Get Collection Through 2 Pivot Tables

Seems like no matter how much I research and try to learn eloquent relationship, I really struggle to get it.

Anyway...

I have 3 models: User, Position, and Content. I have 2 pivot tables: user_position and content_position. Knowing the user_id, I need to get all the contents through that user's positions. I assume it needs to be some has-many-through relationship, but I am stuck.

Appreciate any help.

0 likes
2 replies
Snapey's avatar

On the face of it you would expect this to be a hasManyThrough relation. But this relationship type is using a simpler belongs to relationship

What you need is a hasManyThroughMany which as far as I am aware is only possible by writing your own multi-table join or by using a third party package https://github.com/staudenmeir/eloquent-has-many-deep

dmhall0's avatar

@Snapey Thanks for the reply and the info. Could I do it by pulling and array of the user's positions and then using that in a where statement somehow?

Please or to participate in this conversation.