Laravel 5.5 对查询进行分页 - 预加载
Laravel 5.5 Paginate the Query - Eager Loading
我有一个问题,不知道如何解决。我在网上搜索过,但仍然无法得到确切的答案。我想要做的是对查询 table 或下面的查询进行分页。一切似乎都正常(至少我每页收到 20 个查询,但我不知道如何显示链接()?
我将 Inquiries 保存在 $thecusts 集合中并将其传递给视图。
表和关系:员工 - 经销商(多对多)
经销商-客户(1对多)
客户-查询(1 对多)。
所以我需要为拥有许多经销商的员工分页 x 个查询。
有人可以帮忙吗?
$justch = $me->employeehasdealers()->get(['dealers.dealer_id','dealer','abbreviation']); //list of dealers assigned to auth employee
$justch2=$justch->load(['DealerHasInquiries'=>function($query){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->paginate(20);
}]);
$thecusts = new Collection();
foreach ($justch2 as $just) {
$thecusts = $thecusts->merge($just->DealerHasInquiries);
}
however I can't figure out how to display the links()?
您可以在 blade:
{{ $justch2->links() }}
问题是您没有传回 paginator
实例。您可以根据 $thecusts
:
在您的控制器中手动创建一个
$perPage=20;
return view('my-view', ['paginator' => new Paginator($thecusts->toArray(), $perPage)]);
然后您可以在视图中的 paginator
实例上调用 links 方法:
{{ $paginator->links() }}
编辑
您也许可以在单个查询中简单地完成所有这些操作,例如:
$thecusts = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer');
}])->paginate(20);
return view('my-view, compact('thecusts'));
答案已由 btl 发布 -(非常感谢!)
这是我添加了一些内容的代码:
控制器:
$perpage=19;
$justch = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query) use ($perpage){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->simplePaginate($perpage);
}])->get(['dealers.dealer_id','dealer','abbreviation']);
foreach ($justch as $just) {
$thecusts = $thecusts->merge($just->DealerHasInquiries);
}
$paginator = new Paginator($thecusts->toArray(),$perpage, Paginator::resolveCurrentPage(),['path'=>Paginator::resolveCurrentPath()]);
$inquiries = $thecusts;
return view('/inquiries', ['paginator'=>$paginator,'inquiries' => $inquiries]);
Blade: {{$paginator->links()}}
我有一个问题,不知道如何解决。我在网上搜索过,但仍然无法得到确切的答案。我想要做的是对查询 table 或下面的查询进行分页。一切似乎都正常(至少我每页收到 20 个查询,但我不知道如何显示链接()?
我将 Inquiries 保存在 $thecusts 集合中并将其传递给视图。 表和关系:员工 - 经销商(多对多) 经销商-客户(1对多) 客户-查询(1 对多)。
所以我需要为拥有许多经销商的员工分页 x 个查询。
有人可以帮忙吗?
$justch = $me->employeehasdealers()->get(['dealers.dealer_id','dealer','abbreviation']); //list of dealers assigned to auth employee
$justch2=$justch->load(['DealerHasInquiries'=>function($query){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->paginate(20);
}]);
$thecusts = new Collection();
foreach ($justch2 as $just) {
$thecusts = $thecusts->merge($just->DealerHasInquiries);
}
however I can't figure out how to display the links()?
您可以在 blade:
{{ $justch2->links() }}
问题是您没有传回 paginator
实例。您可以根据 $thecusts
:
$perPage=20;
return view('my-view', ['paginator' => new Paginator($thecusts->toArray(), $perPage)]);
然后您可以在视图中的 paginator
实例上调用 links 方法:
{{ $paginator->links() }}
编辑
您也许可以在单个查询中简单地完成所有这些操作,例如:
$thecusts = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer');
}])->paginate(20);
return view('my-view, compact('thecusts'));
答案已由 btl 发布 -(非常感谢!)
这是我添加了一些内容的代码:
控制器:
$perpage=19;
$justch = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query) use ($perpage){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->simplePaginate($perpage);
}])->get(['dealers.dealer_id','dealer','abbreviation']);
foreach ($justch as $just) {
$thecusts = $thecusts->merge($just->DealerHasInquiries);
}
$paginator = new Paginator($thecusts->toArray(),$perpage, Paginator::resolveCurrentPage(),['path'=>Paginator::resolveCurrentPath()]);
$inquiries = $thecusts;
return view('/inquiries', ['paginator'=>$paginator,'inquiries' => $inquiries]);
Blade: {{$paginator->links()}}