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

Method Illuminate\Database\Eloquent\Collection::paginate does not exist

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

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'));
}

但是我得到这个错误。

Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

我建议你使用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'));
}