laravel分页的多个渴望加载

时间:2019-02-13 06:36:53

标签: laravel eloquent eager-loading

我想返回渴望加载的关系的分页数据,以便可以在视图模板中使用分页显示它们

public function show(Group $group)
{
    $data = ['members', 'assets', 'liabilities', 'loans','shares'];

    $group = $group->load($data);

    return view('admin.groups.show', compact('group'));
}

这是我的小组模型

<?php

namespace App;

class Group extends BaseModel
{   
    use Enumable;

    public function constitution()
    {
        return $this->hasOne(Constitution::class);
    }

    public function members()
    {
        return $this->hasMany(Member::class);
    }


    public function assets()
    {
        return $this->hasManyThrough(Asset::class, Meeting::class)
        ->orderByDesc('meeting_id')
        ->orderBy('member_id');
    }

    public function loans()
    {
        return $this->hasManyThrough(Loan::class, Member::class)
        ->orderByDesc('meeting_id')
        ->orderBy('member_id');
    }

    public function shares()
    {
        return $this->hasManyThrough(Share::class, Member::class)
        ->orderByDesc('meeting_id')
        ->orderBy('member_id');
    }

    public function liabilities()
    {
        return $this->hasOne(Liability::class)->latest();
    }
}

我需要对$ data数组进行分页

1 个答案:

答案 0 :(得分:1)

使用setRelations()

public function show(Group $group)
{
    $group->setRelations([
        'members', $group->members()->paginate(5),
        'assets', $group->assets()->paginate(5),
        'liability', $group->liability()->paginate(5),
        'loans', $group->loans()->paginate(5)
    ]);

    return view('admin.groups.show', compact('group'));
}