/** * @param $status * * @return \Illuminate\Database\Eloquent\Collection */ private function findFriendships($status = '%') { return Friendship::where('status', 'LIKE', $status)->where(function ($query) { $query->where(function ($q) { $q->whereSender($this); })->orWhere(function ($q) { $q->whereRecipient($this); }); }); }
/** * Get the query builder for friendsOfFriends ('friend' model) * * @param string $groupSlug * * @return \Illuminate\Database\Eloquent\Builder */ private function friendsOfFriendsQueryBuilder($groupSlug = '') { $friendships = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']); $recipients = $friendships->pluck('recipient_id')->all(); $senders = $friendships->pluck('sender_id')->all(); $friendIds = array_unique(array_merge($recipients, $senders)); $fofs = Friendship::where('status', Status::ACCEPTED)->where(function ($query) use($friendIds) { $query->where(function ($q) use($friendIds) { $q->whereIn('sender_id', $friendIds); })->orWhere(function ($q) use($friendIds) { $q->whereIn('recipient_id', $friendIds); }); })->whereGroup($this, $groupSlug)->get(['sender_id', 'recipient_id']); $fofIds = array_unique(array_merge($fofs->pluck('sender_id')->all(), $fofs->pluck('recipient_id')->all())); // Alternative way using collection helpers // $fofIds = array_unique( // $fofs->map(function ($item) { // return [$item->sender_id, $item->recipient_id]; // })->flatten()->all() // ); return $this->whereIn('id', $fofIds)->whereNotIn('id', $friendIds); }