Laravel 5.5分析查询 - 渴望加载

时间:2018-02-04 12:05:10

标签: laravel pagination loading eager

我有一个问题,无法弄清楚如何解决它。我在网上搜索但仍无法得到确切的答案。我要做的是分页查询表 - 或下面的查询。一切似乎都有效(至少我每页都有20个查询,但我无法弄清楚如何显示链接()?

我在$ thecusts集合中保存查询并将其传递给视图。 表和关系:员工 - 经销商(manytomany) 经销商 - 客户(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);
}

3 个答案:

答案 0 :(得分:0)

  

但是我无法弄清楚如何显示链接()?

您可以在刀片中执行此操作:

{{ $justch2->links() }}

答案 1 :(得分:0)

问题是你没有传回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'));

答案 2 :(得分:0)

答案已由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]);

刀片:{{$paginator->links()}}