Laravel: paginate() 的结果跨页面不一致

Laravel: Results of paginate() are inconstant across pages

我对如下分页结果有疑问:

要求:

GET http://localhost:1000/api/v1/public/blog/articles

回复:

{
    "current_page": 1,
    "data": [
        {
            "article_id": 43
        },
        {
            "article_id": 107
        },
        {
            "article_id": 171
        },
        {
            "article_id": 22
        },
        {
            "article_id": 86
        },
        {
            "article_id": 150
        },
        {
            "article_id": 1
        },
        {
            "article_id": 65
        },
        {
            "article_id": 129
        },
        {
            "article_id": 44
        }
    ],
    "first_page_url": "http://localhost:1000/api/v1/public/blog/articles?page=1",
    "from": 1,
    "last_page": 18,
    "last_page_url": "http://localhost:1000/api/v1/public/blog/articles?page=18",
    "next_page_url": "http://localhost:1000/api/v1/public/blog/articles?page=2",
    "path": "http://localhost:1000/api/v1/public/blog/articles",
    "per_page": "10",
    "prev_page_url": null,
    "to": 10,
    "total": 179
}

所以,现在如果我在同一个 link 中再次请求,它会显示具有不同 ID 的一组不同的文章。 另外,如果我转到 page=1 或 page=2,每一页的结果都是不稳定的。

我不确定这个问题是从哪里发生的, 我想提一下,我的其他分页查询工作正常,除非我尝试连接查询以获得特定结果,如下所示:

$query = DB::query();

// Then Concatenating other queries based on options

// Now Execute The Whole Query
$fetched_articles = $query->paginate(5);

如果有人能在此处描述分页器流程以及解决方案或解决方法,那将很有帮助。

提前致谢!

根据 Laravel 文档

Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

因此,如果您需要分页,则不能在查询中使用 groupBy

顺便说一句,你可以试试这个 blog post,也许它对你有用。