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

gnabin01's avatar

How to set has many through relation and many to many between three table

I have three table post,widget,widget_collection.Relationship between post and widget is many to many so I have set pivot table between widget and post as post_widget using post_id and widget_id.Widget table also contain widget_collection_id and relationship is one to many.Now i want to access post through widget_collection using hasManyThrough relationship.I set like this in WidgetCollection model: public function posts() { return $this->hasManyThrough('App\Post', 'App\widget','widgets_collection_id','widget_id'); } What is wrong with this code??According to syntax third argument is name of foreign key of intermediate model and fourth argument is foreign key of final model.I have set intermediate foreign key as widgets_collection_id but for fourth i set widget_id but there is no widget_id in post table.I have set many to many relation in post and widget model.Using above code i get error .How can i solve it?? Thanks in Advance :)

0 likes
0 replies

Please or to participate in this conversation.