Which relationship would you choose?

Posted 4 months ago by yoeriboven

My app functions kind of like Twitter. A User would follow Profiles which have Posts. A user doesn't have posts, profiles do.

The User and the Profiles are in a Many to Many relationship (so with a pivot table).

I want to show a timeline with all posts uploaded by the profiles which are followed by the logged in user.

There's two ways this could be done:

  1. Setup a Has Many between Profiles and Posts: $user->profiles->posts
  2. Setup a Many To Many between Posts and Users: $user->posts

The second means there will be one query less (profiles won't be fetched) but it also means another pivot table will have to be added. The first option also feels a lot cleaner.

Which option would you choose?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.