R
R
Roman2019-03-04 04:27:06
Laravel
Roman, 2019-03-04 04:27:06

Organization "add as friends" and "friends with" in Laravel?

The task is typical: to organize a "service of friends" - to friend, unfriend, display a list, etc. Here: How to make a many-to-many relationship of a model to itself in Laravel? jazzus came up with a good solution:

We do hasMany with User on the user's friends table
public function friends(){
        return $this->hasMany('App\Models\Friend, 'user_id', 'id');
    }

Make 2 ids in this table:
Who adds to friends (Auth:id()) field user_id
Whom the user adds to friends - field friend_id
As a result, using the create method, we make an entry when adding to friends. If the user is added as a friend, the same is true.
we get
$user->friends()->get()
$user->friends()->find($id)

However, this is not clear to me:
- And if I need to get a list of users Who I'm friends with - then what?
Those. typical operations here:
- Add to friends
- Remove from friends
- Display a list of friends
- Display a list of "friends with"
I don't understand the last point.
And one more thing - if I want to make a "soft" removal from friends - this is a flag in the second table, right?
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question