Laravel分页json格式

时间:2018-03-16 22:04:06

标签: json laravel pagination

我正在为api使用laravel数据库分页。它输出如下:

{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "name": "Default"
        },
        {
            "id": 2,
            "name": "Default without Sidebar"
        }
    ],
    "first_page_url": "http://example.com/api/layouts?a=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://example.com/api/layouts?a=1",
    "next_page_url": null,
    "path": "http://example.com/api/layouts",
    "per_page": 10,
    "prev_page_url": null,
    "to": 2,
    "total": 2
}

我不想要或不需要first_page_urllast_page_url等,所以应该只是:

{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "name": "Default"
        },
        {
            "id": 2,
            "name": "Default without Sidebar"
        }
    ],
    "from": 1,
    "last_page": 1,
    "to": 2,
    "total": 2
}

那么,有没有办法,格式化输出json,以便我可以删除或隐藏一些我不需要的额外信息。

1 个答案:

答案 0 :(得分:0)

您可以这样做:

$except = ['first_page_url', 'last_page_url'];
return array_except(<YourModel>::paginate(...)->toArray(), $except);