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
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)