Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question