orderBy for GroupBy does not work?

Posted 1 year ago by jamesjames
id  conversation_id      sender_id     recipient_id        message
1   room1         2               1               hi1
2     room1          1                    2                  hi2
3     room1          1                    2                  hi3
4     room2         3                   1                   hi1
5     room2         1                    3                  hi2




public function index(Message $message) 
{
    $messages = $message->where('sender_id', Auth::id())->orWhere('recipient_id', Auth::id())->orderBy('id', 'desc')->groupBy('conversation_id')->get();
    
    //dd($messages);

}

Hi. I am trying to make a private chat system.

I want to display only room1 and room2 and show latest message at http://example.com/inbox.

 room name: room1  
 message: hi3


 room name: room2 
 message: hi2

Everything is fine except message. This does NOT display LATEST message.

Any advice please?

Please sign in or create an account to participate in this conversation.