自定义 Laravel API 分页

问题描述 投票:0回答:1

我正在尝试使我的 API 与此响应匹配:

{
    "data": [
        {
            "id": 1,
            "name": "Emma Smith",
            "avatar": "avatars/300-6.jpg",
            "email": "[email protected]",
            "position": "Art Director",
            "role": "Administrator",
            "last_login": "Yesterday",
            "two_steps": false,
            "joined_day": "10 Nov 2022, 9:23 pm",
            "online": false
        },
        {
            "id": 2,
            "name": "Melody Macy",
            "initials": {
                "label": "M",
                "state": "danger"
            },
            "email": "[email protected]",
            "position": "Marketing Analytic",
            "role": "Analyst",
            "last_login": "20 mins ago",
            "two_steps": true,
            "joined_day": "10 Nov 2022, 8:43 pm",
            "online": false
        },
        {
            "id": 3,
            "name": "Max Smith",
            "avatar": "avatars/300-1.jpg",
            "email": "[email protected]",
            "position": "Software Enginer",
            "role": "Developer",
            "last_login": "3 days ago",
            "two_steps": false,
            "joined_day": "22 Sep 2022, 8:43 pm",
            "online": false
        },
        {
            "id": 4,
            "name": "Sean Bean",
            "avatar": "avatars/300-5.jpg",
            "email": "[email protected]",
            "position": "Web Developer",
            "role": "Support",
            "last_login": "5 hours ago",
            "two_steps": true,
            "joined_day": "21 Feb 2022, 6:43 am",
            "online": false
        },
        {
            "id": 5,
            "name": "Brian Cox",
            "avatar": "avatars/300-25.jpg",
            "email": "[email protected]",
            "position": "UI/UX Designer",
            "role": "Developer",
            "last_login": "2 days ago",
            "two_steps": true,
            "joined_day": "10 Mar 2022, 9:23 pm",
            "online": false
        },
        {
            "id": 6,
            "name": "Mikaela Collins",
            "initials": {
                "label": "M",
                "state": "warning"
            },
            "email": "[email protected]",
            "position": "Head Of Marketing",
            "role": "Administrator",
            "last_login": "5 days ago",
            "two_steps": false,
            "joined_day": "20 Dec 2022, 10:10 pm",
            "online": false
        },
        {
            "id": 7,
            "name": "Francis Mitcham",
            "avatar": "avatars/300-9.jpg",
            "email": "[email protected]",
            "position": "Software Arcitect",
            "role": "Trial",
            "last_login": "3 weeks ago",
            "two_steps": false,
            "joined_day": "10 Nov 2022, 6:43 am",
            "online": false
        },
        {
            "id": 8,
            "name": "Olivia Wild",
            "initials": {
                "label": "O",
                "state": "danger"
            },
            "email": "[email protected]",
            "position": "System Admin",
            "role": "Administrator",
            "last_login": "Yesterday",
            "two_steps": false,
            "joined_day": "19 Aug 2022, 11:05 am",
            "online": false
        },
        {
            "id": 9,
            "name": "Neil Owen",
            "initials": {
                "label": "N",
                "state": "primary"
            },
            "email": "[email protected]",
            "position": "Account Manager",
            "role": "Analyst",
            "last_login": "20 mins ago",
            "two_steps": true,
            "joined_day": "25 Oct 2022, 10:30 am",
            "online": false
        },
        {
            "id": 10,
            "name": "Dan Wilson",
            "avatar": "avatars/300-23.jpg",
            "email": "[email protected]",
            "position": "Web Desinger",
            "role": "Developer",
            "last_login": "3 days ago",
            "two_steps": false,
            "joined_day": "19 Aug 2022, 10:10 pm",
            "online": false
        }
    ],
    "payload": {
        "pagination": {
            "page": 1,
            "first_page_url": "/?page=1",
            "from": 1,
            "last_page": 3,
            "links": [
                {
                    "url": null,
                    "label": "« Previous",
                    "active": false,
                    "page": null
                },
                {
                    "url": "/?page=1",
                    "label": "1",
                    "active": true,
                    "page": 1
                },
                {
                    "url": "/?page=2",
                    "label": "2",
                    "active": false,
                    "page": 2
                },
                {
                    "url": "/?page=3",
                    "label": "3",
                    "active": false,
                    "page": 3
                },
                {
                    "url": "/?page=2",
                    "label": "Next »",
                    "active": false,
                    "page": 2
                }
            ],
            "next_page_url": "/?page=2",
            "items_per_page": 10,
            "prev_page_url": null,
            "to": 10,
            "total": 21
        }
    }
}

来源 - https://preview.keenthemes.com/theme-api/api/users/query/

目前我正在使用标准的 Laravel 分页:

return Users::paginate();

但是,它不会将 JSON 响应分解为我正在使用的前端应用程序正在寻找的内容:

"payload": {
        "pagination": {
json laravel pagination crud metronic
1个回答
0
投票

为了匹配所需的 JSON 响应结构,您需要自定义 Laravel 的分页输出。您可以通过按照前端应用程序所需的格式手动构建分页数据来实现此目的。具体方法如下:

$users = Users::paginate();

// Extracting the pagination data from the paginator instance
$pagination = [
    'page' => $users->currentPage(),
    'first_page_url' => $users->url(1),
    'from' => $users->firstItem(),
    'last_page' => $users->lastPage(),
    'links' => array_map(function ($url, $page) use ($users) {
        return [
            'url' => $url,
            'label' => $page,
            'active' => $page == $users->currentPage(),
            'page' => $page
        ];
    }, $users->getUrlRange(1, $users->lastPage()), range(1, $users->lastPage())),
    'next_page_url' => $users->nextPageUrl(),
    'items_per_page' => $users->perPage(),
    'prev_page_url' => $users->previousPageUrl(),
    'to' => $users->lastItem(),
    'total' => $users->total(),
];

// Combine the users data and the pagination data into the desired structure
$response = [
    'data' => $users->items(), // Users data
    'payload' => [
        'pagination' => $pagination // Pagination data
    ]
];

return response()->json($response);

在此代码中:

  • 我们使用
    Users::paginate()
    检索用户。
  • 然后,我们使用 Laravel 分页器实例提供的方法手动构造所需格式的分页数据。
  • 最后,我们将用户数据和分页数据组合成所需的结构,并将其作为 JSON 响应返回。

此方法可确保分页输出与前端应用程序期望的结构相匹配。根据需要调整键和值以满足您的特定要求。

© www.soinside.com 2019 - 2024. All rights reserved.