mardi 30 avril 2019

Order collection by newest relationship model Laravel

I have two models Chat and ChatMessage.

Chat has many ChatMessage, i want to get all the Chat ordered by the ChatMessage created_at

So if the created_at of the ChatMessage has a date closest to today, that chat should be ordered first.

how could this be done?

I just have this

Chat::leftJoin('chat_messages', 'chats.id', '=', 'chat_messages.chat_id')
->orderBy('chat_messages.id', 'DESC');

but this does not work. i am not getting the newest row of chat_messages.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire