With our User model we are eager loading conversations and messages of these conversations. Of each conversation, we would like to lead the last 25 messages. So we wrote the following query.
User::where('status', 3)->with(['conversations.messages' => function ($q) {
$q->take(25)->orderBy('created_at', 'DESC');
}])->get();
However, this function only returns 25 messages in total (so not per conversation). Even if you have a 1000 users it will load the last 25 messages in the messages table but not the last 25 per conversation of a user.
Is there a way to solve this within the Laravel Eloquent methodology?