/** * Get the query builder of the 'friend' model * * @return \Illuminate\Database\Eloquent\Builder */ private function getMutualFriendsQueryBuilder(Model $other) { $user1['friendships'] = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']); $user1['recipients'] = $user1['friendships']->pluck('recipient_id')->all(); $user1['senders'] = $user1['friendships']->pluck('sender_id')->all(); $user2['friendships'] = $other->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']); $user2['recipients'] = $user2['friendships']->pluck('recipient_id')->all(); $user2['senders'] = $user2['friendships']->pluck('sender_id')->all(); $mutualFriendships = array_unique(array_intersect(array_merge($user1['recipients'], $user1['senders']), array_merge($user2['recipients'], $user2['senders']))); return $this->whereNotIn('id', [$this->getKey(), $other->getKey()])->whereIn('id', $mutualFriendships); }