Skip to content

Commit

Permalink
use less queries
Browse files Browse the repository at this point in the history
  • Loading branch information
merodiro committed May 16, 2018
1 parent cfb79ea commit aaa608d
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit aaa608d

Please sign in to comment.