在GROUP_BY中获取最新消息

I am trying to group a bunch of Messages by the Users who sent them. My current solution gives me the oldest Message in the groups.

$user->messages()->groupBy('user_from')->get();

How can I achieve the reversed messages?

@Maeh,

If you are using timestamps() while migration then you could do orderBy('FIELD_NAME', 'DESC')

Assuming your Table has an ID, you can order by the primary key ID ascending to get the messages that are near the top.

Try orderBy('columnName', 'desc '); after your groupBy('user_form')

where columnName should be that column which have time

You are making it too hard.

$user->messages()->orderBy('created_date', 'DESC')->first();

No need to group because you are already grabbing messages by the user.