Eloquent Eager Loading Constraint Limit

≡放荡痞女 提交于 2019-12-12 14:13:06

问题


I have a Post and Comment model.

Post has a hasMany relationship to Comment. Comment has a belongsTo relationship to Post.

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();

But this constraint will only load 3 comments for all the 10 posts instead of 3 comments per post.

If this is not yet possible via Eloquent, is there any other solution that also implements eager loading?

Thanks


回答1:


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.



来源:https://stackoverflow.com/questions/22044954/eloquent-eager-loading-constraint-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!