From aaa608d434b47b99f60282b34da1d645158f6c36 Mon Sep 17 00:00:00 2001 From: "Amr A. Mohamed" Date: Wed, 16 May 2018 03:07:24 +0200 Subject: [PATCH] use less queries --- src/Friendable.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Friendable.php b/src/Friendable.php index 0193e87..e006ad5 100644 --- a/src/Friendable.php +++ b/src/Friendable.php @@ -71,19 +71,18 @@ public function deleteFriend($user) public function friends() { - $recipients = Friendship::whereSender($this) - ->accepted(1) - ->get(['user_requested']) - ->toArray(); - - $senders = Friendship::whereRecipient($this) - ->accepted(1) - ->get(['requester']) - ->toArray(); + $friendsIds = Friendship::where(function ($query) { + $query->whereSender($this); + })->orWhere(function ($query) { + $query->whereRecipient($this); + })->accepted(1)->get(['user_requested', 'requester'])->toArray(); - $friendsIds = array_merge($recipients, $senders); + $friendsIds = collect($friendsIds)->flatten()->unique()->reject(function ($id) { + return $id == $this->id; + }); return static::whereIn('id', $friendsIds) + ->distinct() ->get(); }