I have a βpostsβ table with the following columns
CREATE TABLE `messages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `fromId` int(11) NOT NULL, `toId` int(11) NOT NULL, `message` text NOT NULL, `status` int(11) NOT NULL, `device` varchar(100) NOT NULL, `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=latin1;
I am trying to get all messages where 'toId' = $ id and grouping byId. The problem is that the βmessageβ shown on the results is the first, not the last. I tried sorting by createdAt, but it does not work.
How can I order "createdAt" before querying and grouping the results? I want to do this with larvel using Eloquent.
My request:
$chats = Message::with('sender','recipient') ->where('toId',$id) ->orderBy('createdAt') ->groupBy('fromId') ->paginate(10)
source share