首页 > 解决方案 > Laravel paginate(10) 不适用于第二页

问题描述

我正在尝试 Laravel 的分页功能,但下面的 URL 每次都返回相同的记录。这意味着无论我们在哪个页面上,它都只显示前 10 条记录。我不知道为什么。

网址 - https://example.com/api/getWebGalleryImages?page=3

我的控制器功能

public function getWebGalleryImages() {
      $galleryFIle = WebGallery::orderBy('id', 'desc')->paginate(10);
      return response()->json($galleryFIle, 200);
    }

我的路线

Route::get('getWebGalleryImages', 'FrontController@getWebGalleryImages');

每次都是一样的反应

{
    "current_page": 1,
    "data": [
        {
            "id": 11,
            "file_name": "1627985668_download(2).jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 10,
            "file_name": "1627985646_istockphoto-1093301674-612x612.jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 9,
            "file_name": "1627985646_images(4).jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 8,
            "file_name": "1627985646_images(3).jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 7,
            "file_name": "1627985645_images(2).jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 6,
            "file_name": "1627985645_images(1).jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 5,
            "file_name": "1627985645_download(2).jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 4,
            "file_name": "1627985645_download(1).jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 3,
            "file_name": "1627985644_Diamond.jpg",
            "created_at": null,
            "updated_at": null
        },
        {
            "id": 2,
            "file_name": "1627985644_depositphotos_3929711-stock-photo-diamond-on-black-background.jpg",
            "created_at": null,
            "updated_at": null
        }
    ],
    "first_page_url": "https://example.com/api/getWebGalleryImages?page=1",
    "from": 1,
    "last_page": 2,
    "last_page_url": "https://example.com/api/getWebGalleryImages?page=2",
    "next_page_url": "https://example.com/api/getWebGalleryImages?page=2",
    "path": "https://example.com/api/getWebGalleryImages",
    "per_page": 10,
    "prev_page_url": null,
    "to": 10,
    "total": 11
}

标签: phplaravel

解决方案


从您的问题的响应中,您的查询似乎有11条匹配的记录,并且您的每页结果设置为10 ,这意味着您只有2页记录。因此,在您的查询中调用page=3不会为您提供新记录,因为该页面不可用。

如果您需要分页工作,请将每页结果设置为较低的值,例如3或向记录中添加更多数据。


推荐阅读