Eager Loading with Pagination

后端 未结 2 778
猫巷女王i
猫巷女王i 2021-01-17 08:08

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

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


        
相关标签:
2条回答
  • 2021-01-17 08:36

    Use setRelation() and lazy loading:

    public function show(Group $group)
    {
        return $group->setRelation('members', $group->members()->paginate(5));
    }
    
    0 讨论(0)
  • 2021-01-17 08:42

    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;
    }
    
    0 讨论(0)
提交回复
热议问题