我见过很多帖子,但没有什么是蠢蠢欲动的。似乎总是将最老的一组分组。我想对结果进行分组并选择最新的结果。 这是我的查询
Message::whereIn('conversation_id',$msgs)
->orderBy('created_at', 'desc')
->groupBy('conversation_id')
->with(array('last_sender'=>function($query){
$query->select('id','userName','profilePic', 'firstName','lastName');
}))
->get();
答案 0 :(得分:1)
试试这个
Message::whereIn('conversation_id',$msgs)
->with(array('last_sender'=>function($query){
$query->select('id','userName','profilePic', 'firstName','lastName');
}))
->orderBy('conversation_id', 'desc')
->groupBy('conversation_id')
->get();
答案 1 :(得分:1)
要按时间排序,请尝试以下方法:
Message::select(DB::raw('MAX(created_at) AS max_created_at'))
->whereIn('conversation_id',$msgs)
->orderBy('max_created_at', 'desc')
->groupBy('conversation_id')
->with(array('last_sender'=>function($query){
$query->select('id','userName','profilePic', 'firstName','lastName');
}))
->get();