Eager Loading with Pagination

大憨熊 提交于 2019-12-01 13:32:49

问题


I have a a group instance I want to eager load with pagination:

public function show(Group $group)
{
    $group = $group->with('members);
    return$ group     
}

I want to return group with members paginated.


回答1:


Use setRelation() and lazy loading:

public function show(Group $group)
{
    return $group->setRelation('members', $group->members()->paginate(5));
}



回答2:


You can do that but it's not recommended because using paginate for eager loaded relationships generates two repeated queries

public function show(Group $group)
{
     $group = $group->with(['members' => function($query) {
        $query->paginate(24);
    }]);

     return $group;
}


来源:https://stackoverflow.com/questions/54646763/eager-loading-with-pagination

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