首页 > 解决方案 > 为什么 laravel 分页只适用于 reture 而不是自定义响应处理程序

问题描述

如果你看到这段代码:

use ResponseHandler;
    public function search(SoundFilter $soundFilter) {

        $sounds = Sound::query();
        $sounds->filter($soundFilter);
//        dd($sounds->toSql());
        $sounds = $sounds->paginate(1);
        $this->setResponseStatus(1);
        $this->setResponseStatusCode(200);
        $this->setResponseData([
            'sounds'    => new SoundsCollection($sounds),
        ]);
        $this->ajaxResponse();
        return $this->response();
    }

我想要soundsCollection($sounds) 返回分页结果,但它只会发送数据

{
    "status": 1,
    "messages": [],
    "statusCode": 200,
    "data": {
        "sounds": [
            {
                "id": 1,
                "name": "منی که عشق با روضه فهمیدم",
                "slug": "",
                "type": 2,
                "description": "<p dir=\"rtl\" style=\"text-align:center\"><strong>متن شعر<\/strong><\/p>",
                "likes": 0,
                "downloads": 0,
                "views": 0,
                "created_at": "2019-08-02 20:09:45",
                "updated_at": "2019-08-03 18:45:49",
                "sort_order": 0,
                "status": 1,
            }
        ]
    }
}

但是当我执行此代码时:

$sounds = Sound::query();
        $sounds->filter($soundFilter);
//        dd($sounds->toSql());
        $sounds = $sounds->paginate(1);
        $this->setResponseStatus(1);
        $this->setResponseStatusCode(200);
//        $this->setResponseData([
////            'sounds'    => new SoundsCollection($sounds),
////        ]);
        $this->ajaxResponse();
        return new SoundsCollection($sounds);

我会得到分页结果

{
    "data": [
        {
            "id": 1,
            "name": "منی که عشق با روضه فهمیدم",
            "slug": "",
            "type": 2,
            "description": "<p dir=\"rtl\" style=\"text-align:center\"><strong>متن شعر<\/strong><\/p>",
            "likes": 0,
            "downloads": 0,
            "views": 0,
            "created_at": "2019-08-02 20:09:45",
            "updated_at": "2019-08-03 18:45:49",
            "sort_order": 0,
            "status": 1,
        }
    ],
    "links": {
        "first": "http:\/\/madahi.test\/api\/v1.0\/search?page=1",
        "last": "http:\/\/madahi.test\/api\/v1.0\/search?page=3",
        "prev": null,
        "next": "http:\/\/madahi.test\/api\/v1.0\/search?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 3,
        "path": "http:\/\/madahi.test\/api\/v1.0\/search",
        "per_page": 1,
        "to": 1,
        "total": 3
    }
}

我应该调用哪个函数或者我应该做什么来获得相同的结果但是使用我的自定义响应处理程序如何强制收集返回分页结果我希望我有链接,第一个结果中的元

标签: phplaravellaravel-pagination

解决方案


如果有同样的问题,我找到了它并分享给其他人:因为我使用单独的 ResponseHandler 并且它会重新发送一个 JsonResponse 对象,它是我发送给 responseHandler 的数据

// trait :  ResponseHandler.php
new JsonResponse($json, $json['statusCode'], $this->headers);

当我调用此代码时,它将重新调整另一个 JsonResponse

'sounds'    => new SoundsCollection($sounds),
// Or
'sounds'    => (new SoundsCollection($sounds))->toResponse($request)

所以上面的 JsonResponse 将忽略分页数据,解决方案是:

 'sounds'    => (new SoundsCollection($sounds))->toResponse($request)->getData()

它会起作用

{
    "status": 1,
    "messages": [],
    "statusCode": 200,
    "data": {
        "sounds": {
            "data": [
                {
                    "id": 1,
                    "name": "منی که عشق با روضه فهمیدم",
                    "slug": "",
                    "type": 2,
                    "description": "<p dir=\"rtl\" style=\"text-align:center\"><strong>متن شعر<\/strong><\/p>",
                    "likes": 0,
                    "downloads": 0,
                    "views": 0,
                    "created_at": "2019-08-02 20:09:45",
                    "updated_at": "2019-08-03 18:45:49",
                    "sort_order": 0,
                    "status": 1,
                }
            ],
            "links": {
                "first": "http:\/\/madahi.test\/api\/v1.0\/search?page=1",
                "last": "http:\/\/madahi.test\/api\/v1.0\/search?page=3",
                "prev": null,
                "next": "http:\/\/madahi.test\/api\/v1.0\/search?page=2"
            },
            "meta": {
                "current_page": 1,
                "from": 1,
                "last_page": 3,
                "path": "http:\/\/madahi.test\/api\/v1.0\/search",
                "per_page": 1,
                "to": 1,
                "total": 3
            }
        }
    }
}


推荐阅读