laravel 中的分页多重预加载
Multiple Eager Loading With Pagination in laravel
我想 return 预加载关系的分页数据,以便我可以在我的视图模板中使用分页显示它们
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 数组进行分页
使用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'));
}
我想 return 预加载关系的分页数据,以便我可以在我的视图模板中使用分页显示它们
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 数组进行分页
使用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'));
}