Answer the question
In order to leave comments, you need to log in
How to sort conversations in laravel by the date of the latest messages in them?
There are Chat models (correspondence) and related ChatMessage models (messages in correspondence). Now on the page with lists of correspondence, they are sorted by id. You need to display them in the order of added messages - the later the date of the last message in the correspondence, the higher it should be in the list. Tell me how to do it.
Answer the question
In order to leave comments, you need to log in
The most correct solution is to specify in ChatMessage that this model is touches Chat and sort chats by updated_at .
If your chats can be edited somehow (i.e. updated_at depends not only on messages), then you can create an additional column for Chat and update it with each message.
All this will be much easier than joining messages and getting the date of the last one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question