Skip to content

Commit aaa608d

Browse files
committed
use less queries
1 parent cfb79ea commit aaa608d

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/Friendable.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,18 @@ public function deleteFriend($user)
7171

7272
public function friends()
7373
{
74-
$recipients = Friendship::whereSender($this)
75-
->accepted(1)
76-
->get(['user_requested'])
77-
->toArray();
78-
79-
$senders = Friendship::whereRecipient($this)
80-
->accepted(1)
81-
->get(['requester'])
82-
->toArray();
74+
$friendsIds = Friendship::where(function ($query) {
75+
$query->whereSender($this);
76+
})->orWhere(function ($query) {
77+
$query->whereRecipient($this);
78+
})->accepted(1)->get(['user_requested', 'requester'])->toArray();
8379

84-
$friendsIds = array_merge($recipients, $senders);
80+
$friendsIds = collect($friendsIds)->flatten()->unique()->reject(function ($id) {
81+
return $id == $this->id;
82+
});
8583

8684
return static::whereIn('id', $friendsIds)
85+
->distinct()
8786
->get();
8887
}
8988

0 commit comments

Comments
 (0)