0

我有一个问题,不知道如何解决它。我在网上搜索过,但仍然没有得到确切的答案。我要做的是对查询表或下面的查询进行分页。一切似乎都有效(至少我每页收到 20 个查询,但是我不知道如何显示链接()?

我将查询保存在 $thecusts 集合中并将其传递给视图。表和关系:员工-经销商(多对多)经销商-客户(一对多)客户-查询(一对多)。

所以我需要为有很多经销商的员工分页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);
}
4

3 回答 3

0

但是我不知道如何显示链接()?

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

{{ $justch2->links() }}
于 2018-02-04T12:13:39.193 回答
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'));
于 2018-02-04T12:43:15.150 回答
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()}}

于 2018-02-04T13:17:00.910 回答