sasani72's avatar

How to implement family and friends relationships for users?

I have a user model and these users have some sorts of relationships together.

Example: John is Jack and Jill's father.

Jack and Jill are siblings.

Jack is the friend of Jacob and Joshua.

I want this to be neat and the solution to make a pivot like (user_one, user_two, relation_one, relation_two) doesn't seem clean to me and becomes repetitive.

How can I implement this kind of relationship altogether? It's a mixture of family relationships and friendships so I'm confused what is the BEST practice for this?

Note: Users can not make relations by themselves. Admin creates all of the relations. It's a report website and I want to show related users (father, siblings, friends ) when visiting a user info page.

0 likes
7 replies
sasani72's avatar

@sr57 I know how Laravel and Eloquent relationships work. I'm just stuck on how to make the friendship AND family combined together and in a cleaner way. I already have a solution that I think is repetitive.

sr57's avatar

I suppose friendship is reciprocate (he is my friend and I am his friend) and not unique (people (can) have more than one friend) : so ManyToMany

Family is a well known relation OneToMany (a parent can have several children)

That said a model can have several relations.

Now, just code.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

Its not like a tree that you can traverse because friends are much more free flowing. EG Jacob might be friends with BOTH Jack and John

Seems to me that you need a pivot table with the relationships and a column that describes the nature of the relationship

1	2	Father
2	1	Daughter
6	1	Friend
6	4	Sibling
4   6   Sibling
2	3	Partner

etc

1 like
sr57's avatar

@Snapey

Not ideal solution for Family tree, need extra code to ensure consistency, isn'it?

Please or to participate in this conversation.