0

I have a chat app with multiple chats in one page. Each chat I want to get the latest 10 messages to load first (and then lazy load the older ones if they want them) when the page loads.

I want to use the whereIn that laravel offers so I can get multiple chat groups in a single query.

My current code is:

$chats = ChatMessage::whereIn('group_id', $group_ids)->orderBy('message_date', 'ASC')->take(10)->get();

But I realize that the take(10) only takes 10. I want to take 10 for every group_id in the table.

What is the most efficient way to do this in laravel 5.1?

Haring10
  • 1,517
  • 1
  • 19
  • 37

1 Answers1

0

I dont think you can do that unless you perform a different query for each chat. In mysql you need use variable to tag each row in the category with a row_number first

Use something like this Row number per group in mysql and then perform your query over that result where row_number <= 10

Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118