首页 > 解决方案 > 在 laravel 分页集合中发送错误的对象

问题描述

我使用下面的代码对数组进行分页,因此对于第一页,一切都很好,并且响应显示正确,但是对于下一页,我有一个问题,在分页数组中为每个我不想要的对象添加一个键那我该如何解决呢?

public function currentOrders(Request $request)
{
    $user = $request->user();
    $orders = Order::with('address','foods')->where(['order_status'=>0,'user_id' => $user->id])->get();
    $data=[];
    if (sizeof($orders)>0) {
        foreach ($orders as $order){
            $tmp['date'] = Jalalian::forge($order->created_at)->format('%B %d، %Y');
            $tmp['time'] = Jalalian::forge($order->created_at)->format('H:i');
            $tmp['total'] = $order->sum_price;
            $tmp['discount_pay'] = $order->price_off;
            $address = Address::withTrashed()->whereId($order->address_id)->first();

            $tmp['address'] = optional($address)->address;
            $tmp['foods'] = [];
            foreach ($order->foods as $food){
                $price = 0;
                $foodDetail = Article::withTrashed()->find($food->food_id);
                $products = OrderItem::with('product')->where(['order_id'=>$food->order_id,'food_id'=>$food->food_id])->get();
                $sec_tmp['products'] = [];
                foreach ($products as $product){
                    $third_tmp['product_name'] = $product->product->title;
                    $third_tmp['count'] = $product->count;
                    $product_price = ($product->product->price->price_off ? $product->product->price->price_off : $product->product->price->customer_price) * $product->count;
                    $third_tmp['price'] = $product_price;
                    $price += $product_price;
                    array_push($sec_tmp['products'] ,$third_tmp );
                }
                $sec_tmp['food_name'] = $foodDetail->title;
                $sec_tmp['food_count'] = $food->count;
                $sec_tmp['food_price'] = $price * $food->count;

                array_push($tmp['foods'],$sec_tmp);

            }
            array_push($data,$tmp);
        }
    }
    $data_collection = collect($data);
    $data_paginated = $this->paginate($data_collection);
    return response()->json(['orders'=>$data_paginated]);
}

public function paginate($items, $perPage = 2, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

我目前的结果是

{
"orders": {
    "current_page": 1,
    "data": [
        {
            "date": "november 19، 2020",
            "time": "01:38",
            "total": 976400,
            "discount_pay": 926400,
            "address": "main st",
            "foods": [
                {
                    "products": [
                        {
                            "product_name": "oil",
                            "count": 3,
                            "price": 15000
                        },
                        {
                            "product_name": "egg",
                            "count": 5,
                            "price": 362000
                        },
                        {
                            "product_name": "fish",
                            "count": 1,
                            "price": 111200
                        }
                    ],
                    "food_name": "sosage",
                    "food_count": 2,
                    "food_price": 976400
                }
            ]
        },
        {
            "date": "november 19، 2020",
            "time": "01:43",
            "total": 976400,
            "discount_pay": 926400,
            "address": "main street",
            
        }
    ],
    "first_page_url": "/?page=1",
    "from": 1,
    "last_page": 2,
    "last_page_url": "/?page=2",
    "next_page_url": "/?page=2",
    "path": "/",
    "per_page": 2,
    "prev_page_url": null,
    "to": 2,
    "total": 4
}

}

但我在接下来的页面中是:

{
"orders": {
    "current_page": 2,
    "data": {
        "1": {
            "date": "november 19، 2020",
            "time": "01:43",
            "total": 976400,
            "discount_pay": 926400,
            "address": "main st",
            "foods": [
                {
                    "products": [
                        {
                            "product_name": "egg",
                            "count": 3,
                            "price": 15000
                        },
                        {
                            "product_name": "oil",
                            "count": 5,
                            "price": 362000
                        },
                        {
                            "product_name": "fish",
                            "count": 1,
                            "price": 111200
                        }
                    ],
                    "food_name": "susage",
                    "food_count": 2,
                    "food_price": 976400
                }
            ]
        }
    },
    "first_page_url": "/?page=1",
    "from": 2,
    "last_page": 4,
    "last_page_url": "/?page=4",
    "next_page_url": "/?page=3",
    "path": "/",
    "per_page": 1,
    "prev_page_url": "/?page=1",
    "to": 2,
    "total": 4
}

}

在数据对象中,有一个像“1”这样的键:{...},我想和第一页一样,我该怎么办?

标签: phplaravelcollectionseloquentlaravel-8

解决方案


$data 是一个数组,但你得到一个键为 1 的对象。它看起来像页码本身。但是你应该在 $data 中有一个 $temp 的集合;

您可以在返回之前检查 $data_paginated['data'] 是否是一个数组 return response()->json(['orders'=>$data_paginated]);

我认为错误出现在 $this->paginate($data_collection) 上,其中 $data_collection 是 $temp 列表中的项目集合。或者可能在 collect($data) 上。我在其他地方没有看到错误。


推荐阅读