php - 在 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”这样的键:{...},我想和第一页一样,我该怎么办?
解决方案
$data 是一个数组,但你得到一个键为 1 的对象。它看起来像页码本身。但是你应该在 $data 中有一个 $temp 的集合;
您可以在返回之前检查 $data_paginated['data'] 是否是一个数组 return response()->json(['orders'=>$data_paginated]);
我认为错误出现在 $this->paginate($data_collection) 上,其中 $data_collection 是 $temp 列表中的项目集合。或者可能在 collect($data) 上。我在其他地方没有看到错误。
推荐阅读
- typescript - 在 Typescript 中,为什么 JSX.IntrinsicElements 不像文档描述的那样工作?
- java - 如何在android中检查txt文件中的一行是否以“h”开头?
- android - 如何在多个活动上实现 admob 横幅
- c# - 使用 AForge 裁剪时特定计算机上的奇怪行为
- php - 尝试在重置用户密码时获取非对象的属性
- javascript - 将数据从 web 服务加载到表中,然后到数据库
- php - 在PHP中合并两个具有公共字段值的json文件
- php - 不需要的前缀被添加到我的查询列名中
- javascript - JavaScript ES6 转换复杂对象的更好方法
- python - Project Euler 问题 #18 Python - 得到错误的结果。为什么?