Skip to content

Commit

Permalink
Merge pull request #6 from jlozano254/master
Browse files Browse the repository at this point in the history
Reduces memory usage & CPU processing, fix for empty collection return
  • Loading branch information
elbakly authored Feb 19, 2018
2 parents 8295cb6 + 6ab560e commit 82dbda3
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/Messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function messagesWith($authId, $withId, $take = 20)
$conversation = $this->getConversation($authId, $withId);

if ($conversation) {
$collection = Message::whereConversationId($conversation->id)
$messages = Message::whereConversationId($conversation->id)
->where(function ($query) use ($authId, $withId) {
$query->where(function ($qr) use ($authId) {
$qr->where('sender_id', $authId) // this message is sent by the authUser.
Expand All @@ -130,16 +130,15 @@ public function messagesWith($authId, $withId, $take = 20)
$qr->where('sender_id', $withId) // this message is sent by the receiver/withUser.
->where('deleted_from_receiver', 0);
});
});
$totalRecords = $collection->count();
$messages = $collection->take($take)
->skip($totalRecords - $take)
})
->latest()
->take($take)
->get();

return $messages;
return $messages->reverse();
}

return null;
return collect();
}

/**
Expand Down

0 comments on commit 82dbda3

Please sign in to comment.