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

chern123's avatar

Relationship between two tables via two separate pivots

I'm having a tough time wrapping my head around this problem. Assume there are two tables:

-Group (which has a pivot table group_members, which takes into account different types of users, etc)
-Notifications

Now, the relationship I wanted to model was between Notifications and group_members, as every group member can be the recipient of a notification, and a single notification can be sent out to multiple recipients. This seems to require another pivot table: notification_group_members.

So, really, what it seems to be is that I have a relationship between two pivot tables. It would be easy to come up with a solution simply using DB::(), but I would prefer to use the existing models I have, and not have to create new models for the pivot tables. Is there a way to do this? Any ideas?

0 likes
2 replies
pmall's avatar

@mdobrenko @cyril.potdevin@gmail.com

Think this another way : group_members is in fact, a model on its own. You can call it Subscription for example. Then :

  • Groups has Many Subscription
  • Member has Many Subscription
  • Subscription belongs To Many Notification

All notifications for a member :

foreach ($member->subscriptions as $subscription) {

    foreach ($subscription->notifications as $notification) {

        //

    }

}

Please or to participate in this conversation.