Get most recent row with group by and Laravel

前端 未结 5 1759
悲哀的现实
悲哀的现实 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:29

    You may replace groupBy with distinct, as it works in my case.

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

    Hope this helps.

提交回复
热议问题