bootstrapguru's avatar

Get latest messages and then reverse the order

Hi Guys, Hope everyone doing great. I am stuck with some query which seems to be easy but still I am not able to get it.

I have Private chat system where I am trying to get latest messages from conversation with pagination. The problem is I can get latest 10 messages but unable to reverse them

$thread->conversationMessages = $thread->messages()->latest()->with('user')->paginate(10);

above line gives me the data with paginator collection so if I use reverse method on collection, I am loosing the pagination properties. Please let me know if have explained bad,

0 likes
4 replies
kobear's avatar

Are you using blade to render the text? If so, you can reverse it there.

@foreach ($messages->reverse() as $message)
    // blah blah blah
@endforeach 

or if you are not going to be rendering it, you can reverse the collection.

$tmpCollection = $thread->messages()->latest()->with('user')->paginate(10);
$thread->conversationMessages = $tmpCollection->reverse();
kobear's avatar

@bootstrapguru then you can try to run the reverse() against the collection directly, like my second example.

bootstrapguru's avatar

Then it looses pagination data and returns only the items array. I don't want loose attributes like last_page, total,etc

Thanks for replying

Please or to participate in this conversation.