使用自定义 json 函数检索数据时分页不起作用

Posted

技术标签:

【中文标题】使用自定义 json 函数检索数据时分页不起作用【英文标题】:Pagination not working while retriving data with costom json function 【发布时间】:2022-01-09 15:38:26 【问题描述】:

我正在获取数据以进行分页,但分页不起作用,获取响应添加在下面,如果我返回查询关系,它会检索正确的数据,但是当我将它传递给自定义响应函数时,它只获取数据而不是分页链接

try 
            $type = 'success';
            $status_code = 200;
            $message = 'Posts data listed.';
            $response = PostResource::collection(Post::with(['associate:id,name,avatar', 'comments:id,commenter_id,commentable_id,comment,created_at'])
                ->latest('posts.created_at')
                ->paginate(2));
         catch (Exception $e) 
            $type = 'error';
            $status_code = $e->getCode();
            $message = $e->getMessage();
            $response = false;
        
        return response_data($type, $status_code, $message, $response);

这是我的 response_data 函数代码

function response_data($type, $status, $message = false, $response)

    return response()->json(['type' => $type, 'status' => $status, 'message' => $message, 'response' => $response]);

但是如果直接返回查询,它会使用分页检索数据 这是集合检索到的我的回复


    "type": "success",
    "status": 200,
    "message": "Posts data listed.",
    "response": [
        
            "id": 32,
            "associate_id": 5,
            "title": "Test Title",
            "content": "Post descriptuoin",
            "image": "https://oppazz.oppazzgiftcode.com/images/posts/1632472717.Laravel.png",
            "created_at": "2 months ago",
            "associate": 
                "name": "Code Logic Technologies Pvt. Ltd.",
                "avatar": "https://oppazz.oppazzgiftcode.com/images/associates/1633002782_logo.png"
            ,
            "comments": [
                
                    "id": 13,
                    "commenter_id": "62",
                    "commentable_id": "32",
                    "comment": "Nice offer",
                    "created_at": "2 months ago",
                    "user": 
                        "id": 62,
                        "name": "Rikesh Shakya",
                        "username": "rikesh-shakya",
                        "mobile_number": "9823783191",
                        "email": "skybks5@gmail.com",
                        "provider_id": null,
                        "avatar": "https://oppazz.oppazzgiftcode.com/images/logo.png",
                        "email_verified_at": null,
                        "status": "Y",
                        "created_at": "2021-06-11T18:05:07.000000Z",
                        "updated_at": "2021-06-11T18:05:07.000000Z",
                        "created_by": null,
                        "updated_by": null,
                        "device_type": null
                    
                ,
                
                    "id": 16,
                    "commenter_id": "88",
                    "commentable_id": "32",
                    "comment": "tetetete",
                    "created_at": "2 months ago",
                    "user": 
                        "id": 88,
                        "name": "Neelam Kera",
                        "username": "neelam-ranjitkar",
                        "mobile_number": "9860322060",
                        "email": "shresthaneelam19@gmail.com",
                        "provider_id": null,
                        "avatar": "https://oppazz.oppazzgiftcode.com/images/logo.png",
                        "email_verified_at": null,
                        "status": "Y",
                        "created_at": "2021-07-15T14:08:21.000000Z",
                        "updated_at": "2021-07-15T14:08:21.000000Z",
                        "created_by": null,
                        "updated_by": null,
                        "device_type": null
                    
                
            ],
            "associate_social_sites": 
                "id": 5,
                "associate_id": 5,
                "facebook": "https://www.fachook.com",
                "instagram": "https://www.instagram.com",
                "twitter": "https://www.twitter.com",
                "status": "Y",
                "created_at": null,
                "updated_at": "2021-09-24T09:29:57.000000Z",
                "created_by": null,
                "updated_by": null,
                "device_type": null
            
        ,
        
            "id": 31,
            "associate_id": 9,
            "title": "OppazZ Coffee For Happiness (Giveaway series)",
            "content": "OppazZ",
            "image": "https://oppazz.oppazzgiftcode.com/images/posts/1632367205.kamloops-art-page-2.jpg",
            "created_at": "2 months ago",
            "associate": 
                "name": "OppazZ",
                "avatar": "https://oppazz.oppazzgiftcode.com/images/associates/1622551849_184399208_2242958835839866_1824735327179728878_n.jpg"
            ,
            "comments": [],
            "associate_social_sites": null
        
    ]

如何解决并获取分页链接以及上面获取的数据

【问题讨论】:

定义它是如何不工作的? 只有数据正在获取,但没有显示分页链接。 你用的是哪个 laravel 版本? 我使用的是 laravel 8 版 因为您使用的是 PostResource::collection 方法,所以您正在改变(更改)实际数据。所以如果你删除它,或者如果你修改 PostResource 类以返回你需要的那些元数据。 【参考方案1】:

Laravel 分页器结果类实现了Illuminate\Contracts\Support\Jsonable 接口契约并公开了toJson 方法,因此将分页结果转换为 JSON 非常容易。

您也可以将分页器实例转换为 JSON,只需从路由或控制器操作中返回即可:

https://laravel.com/docs/5.3/pagination#converting-results-to-json

【讨论】:

以上是关于使用自定义 json 函数检索数据时分页不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用自定义钩子获取后尝试访问数据不起作用

使用 Ajax 调用的 Extjs Grid 分页不起作用

带有连接表字段排序的 Cakephp 分页不起作用

jquery数据表分页不起作用

片段内的自定义列表不起作用

DataTables - 排序,搜索,分页不起作用