jeudi 31 mars 2016

Fetch last N records Using Jenssegers laravel-mongodb

I want to fetch last 2 inserted chats of users according to the created_at time from the database in the sequence they were inserted using Jenssegers and Mongodb. I have tried some queries but not able to get the desired result.

These queries gives the first two chats in the sequence they were inserted:

$prev_chats = ChatMessages::where('chat_id','=', $chat_id)->take(2)->get();

$prev_chats = ChatMessages::orderBy('created_at','asc')->where('chat_id','=', $chat_id)->take(2)->get();

$prev_chats = ChatMessages::orderBy('created_at','asc')->where('chat_id','=', $chat_id)->paginate(2);

When I try to reverse the order then it fetch the last 2 but the serial in which they were inserted changes(Means Last becomes First).

$prev_chats = ChatMessages::orderBy('created_at','desc')->where('chat_id','=', $chat_id)->paginate(2);

$prev_chats = ChatMessages::orderBy('created_at','desc')->where('chat_id','=', $chat_id)->take(2)->get();

Is there a way to get the last 2 records in the sequence they were inserted.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire