But this is not working as intended.
Can you explain what is the behavior of your code and what is the behavior you are waiting for ?
Hi everyone, after a lot of search I still don't know how to manage friendship relationships in Laravel. What I mean is this: I have a users table, and a friends table. The friends table has, in addition to id and timestamps, user_1_id, user_2_id and the status column (an enum that can be ACCEPTED, DECLINED, PENDING).
It seems like Laravel can't face these kind of relations, for example, having something like $user->friends() (where $user is the instance of a User Model). What I did for now is, by following a tutorial online
public function friendsTo() {
return $this->belongsToMany(User::class, 'friends', 'user_id_1', 'user_id_2')
->withPivot('status')
->withTimestamps();
}
public function friendsFrom() {
return $this->belongsToMany(User::class, 'friends', 'user_id_2', 'user_id_1')
->withPivot('status')
->withTimestamps();
}
public function friends() {
return $this->friendsFrom->merge($this->friendsTo);
}
But this is not working as intendent. Actually it makes hard to filter, query and limit the results.
Do you have any suggestions? What I don't want to do is create another view or table to fix this.
Thanks.
Please or to participate in this conversation.