gpt4 book ai didi

拉维尔 5 : LengthAwarePaginator returned JSON not array

转载 作者:行者123 更新时间:2023-12-01 14:07:45 25 4
gpt4 key购买 nike

我正在从 LengthAwarePaginator 返回 JSON,但 JSON 的 data 属性不是数组。我需要它是一个数组。有什么想法吗?

// grab query parameters
$pageNumber = $request->input('page');
$pageSize = $request->input('pageSize');
// if query params do not exist call with defaults
if(!$pageNumber) {
$pageNumber = 1;
}

if(!$pageSize) {
$pageSize = 5;
}

$offset = ($pageNumber * $pageSize) - $pageSize;
// slice full array data based on page number and page size
$itemsForCurrentPage = array_slice($arrayOfData, $offset, $pageSize, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($this->orgUsers), $pageSize, $pageNumber);

返回数据:

{
"total": 30,
"per_page": 5,
"current_page": 2,
"last_page": 6,
"next_page_url": "/?page=3",
"prev_page_url": "/?page=1",
"from": 6,
"to": 10,
"data": {
"5": {
"userId": "564110eadcb39832268ea873",
"email": "dsdfgdfg@il.com",
"isActive": true,
"firstName": "dsdfgdfg",
"lastName": "dsdfgdfg",
"permissionType": "dsdfgdfg"
},
"6": {
"userId": "564110ea2169bc358a3b65c2",
"email": "dsdfgdfg@d.com",
"isActive": false,
"firstName": "dsdfgdfg",
"lastName": "dsdfgdfg",
"permissionType": "dsdfgdfg"
},
"7": {
"userId": "564110eaee662f30c4bd6772",
"email": "dsdfgdfg@dsdfgdfg.com",
"isActive": true,
"firstName": "dsdfgdfg",
"lastName": "dsdfgdfg",
"permissionType": "dsdfgdfg"
},
"8": {
"userId": "dsdfgdfg",
"email": "dsdfgdfg@dsdfgdfg.com",
"isActive": true,
"firstName": "dsdfgdfg",
"lastName": "dsdfgdfg"
},
"9": {
"userId": "564110eaf9526eb5ddd673a4",
"email": "dsdfgdfg@dsdfgdfg.com",
"isActive": true,
"firstName": "dsdfgdfg",
"lastName": "dsdfgdfg"
}
}
}

TIA

最佳答案

问题是 idsarray_slice 时仍然是数组的键。但是由于缺少一些键,尤其是 0, 1, 2, ... 数组从此被视为关联的 (['key1'=>'value1', 'key2'=>'value2 ', …]) 而不是数字索引 (['value1', 'value2', …]) 在编码为 json 时。

解决方案是在 array_slice(…) 之后调用 array_values()

return new LengthAwarePaginator(array_values($itemsForCurrentPage), count($this->orgUsers), $pageSize, $pageNumber);

编辑:如果您的 $arrayOfData 是 Eloquent/Illuminate Collection,您可以使用方法 ->slice($offset, $pageSize)->values() 就可以了。看起来更好!

关于拉维尔 5 : LengthAwarePaginator returned JSON not array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38712364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com