Closed
Description
We are trying to implement eager loading with eloquent, where we want to get 5 conversations and for each conversation the 10 most recent messages. It seems that Eloquent only applies the limit once, returning 10 messages in total (as opposed to the expected result of up to 10 messages per each of 5 conversations).
The code:
$conversations = ConversationModel::whereIn('id', $conversationsIds)
->with(
array(
'conversationUserData' => function ($query) use ($userId)
{
$query->where('userId', '=', $userId);
},
'messages' => function ($query)
{
$query->take(10);
},
'conversationUsers.user',
'lastMessage.user',
)
)
->take(5)
->orderBy('created_at', 'DESC')
->get();
The problem seems to be the $query->take(10);
part, which seems to apply only "once" on the whole query.
We tried to google on possible causes of this, but while it seems that quite a few people have the same problem, there is no good solution - with the exceptions of workarounds, like fetching the messages programatically, which are not really ellegant.
Is this a bug or is this intended behavior?
Metadata
Metadata
Assignees
Labels
No labels