方法Illuminate \ Database \ Eloquent \ Collection :: paginate不存在

时间:2019-08-02 20:51:27

标签: php laravel laravel-5.8

我想在刀片中合并两个模型。看看我的代码

public function history_posts_requests () {
    $reports = Report::latest()->where('status', '=', '1')->first()->get();
    $order_mobiles = OrderMobile::latest()->where('status', '=', '1')->first()->get();
    $allOrders = $reports->merge($order_mobiles)->paginate(50);
    return view('Admin.desktops.history_posts_requests', compact('allOrders'));
}

但是我得到这个错误。

  

方法Illuminate \ Database \ Eloquent \ Collection :: paginate不存在。

1 个答案:

答案 0 :(得分:3)

我建议您使用PaginatorLengthAwarePaginator类来实现分页:

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

public function history_posts_requests () {
    $reports = Report::latest()->where('status', '=', '1')->first()->get();
    $order_mobiles = OrderMobile::latest()->where('status', '=', '1')->first()->get();
    $allOrders = $reports->merge($order_mobiles);

    $totalGroup = count($allOrders);
    $perPage = 10;
    $page = Paginator::resolveCurrentPage('page');

    $allOrders = new LengthAwarePaginator($allOrders->forPage($page, $perPage), $totalGroup, $perPage, $page, [
        'path' => Paginator::resolveCurrentPath(),
        'pageName' => 'page',
    ]);


    return view('Admin.desktops.history_posts_requests', compact('allOrders'));
}