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

himanshu-dhiman's avatar

Has Many Through query.

I have three models : topic, class and enrollment

Topic :

  1. id
  2. name

class :

  1. id
  2. topic_id
  3. name

enrollment :

  1. id
  2. class_id

I want to query enrollment model, to get the details of topic associated with each class. I am using hasManyThrough relation like following:

return $this->hasManyThrough('Topic', 'Class', 'topic_id', 'id', 'class_id', 'id');

but its not working. Please help. Thanks

0 likes
1 reply
himanshu-dhiman's avatar
Level 1

I solved it myself.

The query will be:

return $this->hasManyThrough('Topic', 'Class', 'id', 'id', 'class_id', 'topic_id')

Please or to participate in this conversation.