gpt4 book ai didi

php - 第 2 页上的 Laravel 自定义分页作为对象返回

转载 作者:行者123 更新时间:2023-12-03 16:14:01 26 4
gpt4 key购买 nike

Laravel 自定义分页第 2 页结果作为对象而不是数组返回。

这是我的自定义分页代码

function custom_paginate($data)
{
//Get current page form url e.g. &page=6
$currentPage = LengthAwarePaginator::resolveCurrentPage();

//Create a new Laravel collection from the array data
$collection = new Collection($data);

//Define how many items we want to be visible in each page
$per_page = (int) per_page();

//Slice the collection to get the items to display in current page
$currentPageResults = $collection->slice(($currentPage - 1) * $per_page, $per_page)->all();

//Create our paginator and add it to the data array
$data['results'] = new LengthAwarePaginator($currentPageResults, count($collection), $per_page);

//Set base url for pagination links to follow e.g custom/url?page=6
return $data['results']->setPath(request()->url());
}

当我在第 1 页时,数据以数组形式出现,但如果我通过 page=2 则以相同的代码出现结果作为对象出现。

预期结果
{
"current_page": 2,
"data": [
{
"_id": 34,
"state": "Andaman & Nicobar Islands",
"post_count": 8,
"totalPhotos": 0,
"totalVideos": 1,
"totalAudios": 0
},
{
"_id": 28,
"state": "Andhra Pradesh",
"post_count": 888,
"totalPhotos": 0,
"totalVideos": 111,
"totalAudios": 0
},
{
"_id": 12,
"state": "Arunachal Pradesh",
"post_count": 200,
"totalPhotos": 0,
"totalVideos": 25,
"totalAudios": 0
},
{
"_id": 18,
"state": "Assam",
"post_count": 464,
"totalPhotos": 0,
"totalVideos": 58,
"totalAudios": 0
},
{
"_id": 10,
"state": "Bihar",
"post_count": 1072,
"totalPhotos": 0,
"totalVideos": 134,
"totalAudios": 0
}
],
"first_page_url": "http://swachh-manch-data.git/insight/posts?page=1",
"from": 6,
"last_page": 8,
"last_page_url": "http://swachh-manch-data.git/insight/posts?page=8",
"next_page_url": "http://swachh-manch-data.git/insight/posts?page=3",
"path": "http://swachh-manch-data.git/insight/posts",
"per_page": 5,
"prev_page_url": "http://swachh-manch-data.git/insight/posts?page=1",
"to": 10,
"total": 36

}

但将数据作为第 2 页上的对象获取( 实际结果 )
{
"current_page": 2,
"data": {
"5": {
"_id": 4,
"state": "Chandigarh",
"post_count": 8,
"totalPhotos": 0,
"totalVideos": 1,
"totalAudios": 0
},
"6": {
"_id": 22,
"state": "Chhattisgarh",
"post_count": 1336,
"totalPhotos": 0,
"totalVideos": 167,
"totalAudios": 0
},
"7": {
"_id": 26,
"state": "Dadra & Nagar Haveli",
"post_count": 8,
"totalPhotos": 0,
"totalVideos": 1,
"totalAudios": 0
},
"8": {
"_id": 25,
"state": "Daman & Diu",
"post_count": 16,
"totalPhotos": 0,
"totalVideos": 2,
"totalAudios": 0
},
"9": {
"_id": 7,
"state": "Delhi",
"post_count": 40,
"totalPhotos": 0,
"totalVideos": 5,
"totalAudios": 0
}
},
"first_page_url": "http://swachh-manch-data.git/insight/posts?page=1",
"from": 6,
"last_page": 8,
"last_page_url": "http://swachh-manch-data.git/insight/posts?page=8",
"next_page_url": "http://swachh-manch-data.git/insight/posts?page=3",
"path": "http://swachh-manch-data.git/insight/posts",
"per_page": 5,
"prev_page_url": "http://swachh-manch-data.git/insight/posts?page=1",
"to": 10,
"total": 36
}

我不确定,但问题出在 $currentPageResults = $collection->slice(($currentPage - 1) * $per_page, $per_page)->all();这条线。

提前致谢。 :)

最佳答案

改变

$currentPageResults = $collection->slice(($currentPage - 1) * $per_page, $per_page)->all();


$currentPageResults = $collection->slice(($currentPage - 1) * $per_page, $per_page)->values();

更多信息阅读 this .

关于php - 第 2 页上的 Laravel 自定义分页作为对象返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929331/

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