Order By before Group By using Eloquent (Laravel)

后端 未结 6 872
无人共我
无人共我 2020-12-03 21:22

I have a \"messages\" table with the following columns

CREATE TABLE `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fromId` int(11) NOT NULL,
  `toId         


        
6条回答
  •  长情又很酷
    2020-12-03 22:22

    Try this query :

    $chats = Message::with('sender','recipient')
    ->where('toId',$id)
    ->whereRaw('id IN (select MAX(id) FROM messages GROUP BY fromId)')
    ->orderBy('createdAt','desc')
    ->paginate(10)
    

提交回复
热议问题