Answer the question
In order to leave comments, you need to log in
How to make a many-to-many relationship of a model to itself in Laravel?
Hello.
I want, for example, to make the "Add to friends" option, and for this I need a many-to-many relationship in the User model, but to myself, right?
What is usually done in such cases?
Thank you.
Answer the question
In order to leave comments, you need to log in
We do hasMany with User on the user's friends table
public function friends(){
return $this->hasMany('App\Models\Friend, 'user_id', 'id');
}
$user->friends()->get()
$user->friends()->find($id)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question