雄辩的渴望加载限制限制 [英] Eloquent Eager Loading Constraint Limit
问题描述
我有一个发表和评论模型.
I have a Post and Comment model.
帖子与评论之间具有hasMany关系. 评论与邮政有一个to be的关系.
Post has a hasMany relationship to Comment. Comment has a belongsTo relationship to Post.
我希望加载带有评论的帖子,但我希望限制为每个帖子只能获得3条评论.我该如何通过Eloquent做到这一点?
I want to eager load posts with their comments, but I want to limit to only get 3 comments per posts. How can I do this by Eloquent?
Post::with(array('comments' => function($query)
{
$query->take(3);
}))->take(10)->get();
但是此约束只会为所有10条帖子加载3条评论,而不是每条帖子3条评论.
But this constraint will only load 3 comments for all the 10 posts instead of 3 comments per post.
如果通过Eloquent还无法做到这一点,是否还有其他解决方案也可以实现预先加载?
If this is not yet possible via Eloquent, is there any other solution that also implements eager loading?
谢谢
推荐答案
这不是laravel限制,而是SQL限制.
This is not a laravel limitation but a SQL limitation.
最好的选择是不使用紧急加载,而是缓存结果以提高性能.
Best option is to not use eager loading and cache the results for performance instead.
这篇关于雄辩的渴望加载限制限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!