如何在Laravel 9中获得带分页的订单列表?

nimxete2  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(118)

“$dbOrderStatusArray”返回包含数据的数组

$dbOrderStatusArray = SitesOrderStatus::where("internal_status_id", 2)->select("site_id", "order_status_id")->get()->toArray();

接收数据的示例如下。

$dbOrderStatusArray = [
    ["site_id" => 7230,"order_status_id" => 17],
    ["site_id" => 7230,"order_status_id" => 23],
    ["site_id" => 7230,"order_status_id" => 25],
    ["site_id" => 3881,"order_status_id" => 2],
    ["site_id" => 3881,"order_status_id" => 19],
    ["site_id" => 3881,"order_status_id" => 17]
];

我试图通过获取数据和收集数据来得到一个按顺序排列的列表。然后我想添加排序和分页。但是无论我多么努力,我还是得到了一个错误。

Method Illuminate\Support\Collection::paginate does not exist.
or
Method Illuminate\Support\Collection::links does not exist..

$orders = collect([]);
foreach ($dbOrderStatusArray as $item) {
$orders = $orders->concat(SitesOrders::where('site_id', $item['site_id'])->where('order_status_id', $item['order_status_id'])->get());
}

$paginate = $orders->sortByDesc('date_added')->paginate($data['paginate']);

$data['orders'] = $paginate;

也许有人遇到了这个问题,可以提供帮助?在开始时,我得到所有商店的订单状态,有id 2的内部网站状态。

py49o6xq

py49o6xq1#

在您的laravel版本中,不能在集合示例上运行paginate()。它应该在QueryBuilder示例上使用。

$orders = SitesOrders::query();
foreach ($dbOrderStatusArray as $item) {
    $orders->orWhere(function($query) use ($item) {
        $query->where('site_id', $item['site_id'])->where('order_status_id', $item['order_status_id']);
    });
}

$paginate = $orders->sortByDesc('date_added')->paginate($data['paginate']);

$data['orders'] = $paginate;

相关问题