Get most recent row with group by and Laravel

前端 未结 5 1768
悲哀的现实
悲哀的现实 2020-12-18 14:34

Even though there are multiple questions like this I can\'t get my query to return the row with the most recent date with a group by.

I have the following table..

5条回答
  •  星月不相逢
    2020-12-18 15:19

    You might also want to orderBy message_id as well

    $messages = Message::where('to', Auth::id())
                        ->groupBy('from')
                        ->orderBy('created_at', 'DESC')
                        ->orderBy('message_id', 'DESC')
                        ->paginate(10);
    

提交回复
热议问题