Implementing an optional two-way user relationship: a step-by-step guide
P粉412533525
P粉412533525 2024-04-01 12:05:59
0
1
398

I have a user model and there is some relationship between these users.

Example: John is the father of Jack and Jill.

Jack and Jill are brothers and sisters.

Jack is a friend of Jacob and Joshua.

How can I fully realize this relationship? This is a mix of family relationships and friendships so I'm confused what is the best course of action?

P粉412533525
P粉412533525

reply all(1)
P粉178894235

You need to make two models: User and Relationship. First, make the two models related (one-to-many relationship):

Model: User

public function relations()
{
    return $this->hasMany(Relation::class);
}

Model:Relationship

public function user()
{
    return $this->belongsTo(User::class);
}

Then in your relational model (remember to set this on your migrations too), you need to have four columns: user_one , user_two , type_one and type_two .

For example: user_one: father's user id / user_two: son's user id, type_one: father / type_two: son.

So be it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!