【问题标题】:Laravel 5.7 Collection get size and offset after paginationLaravel 5.7 Collection 在分页后获取大小和偏移量
【发布时间】:2020-09-09 20:41:32
【问题描述】:

我有一个分页集合,比如

Post::where('active', 1)->paginate(10)

我想获取当前集合部分的开始和结束位置。 我的意思是,如果页面为 2,页面限制为 10,则起始位置为 11,结束位置为 20。我需要在总共 284 个产品中呈现从 11 到 20 的产品文本。有没有可能?

【问题讨论】:

    标签: laravel eloquent pagination laravel-5.7


    【解决方案1】:

    以下方法会返回你想要的信息:

    $pagination = Post::where('active', 1)->paginate(10);
    
    $pagination->firstItem(); // Returns the number of the first item from the current page
    
    $pagination->lastItem(); // Returns the number of the last item from the current page
    
    $pagination->total(); // Returns the total amount of items
    

    你可以找到所有可用的方法here

    如果您将分页转换为数组或 JSON,它将自动添加所有元信息。

    来自docs

    来自分页器的 JSON 将包含元信息,例如 totalcurrent_pagelast_page 等等。实际结果对象 将通过 JSON 数组中的数据键可用。这是一个 通过从 a 返回分页器实例创建的 JSON 示例 路线:

    {
       "total": 50,
       "per_page": 15,
       "current_page": 1,
       "last_page": 4,
       "first_page_url": "http://laravel.app?page=1",
       "last_page_url": "http://laravel.app?page=4",
       "next_page_url": "http://laravel.app?page=2",
       "prev_page_url": null,
       "path": "http://laravel.app",
       "from": 1,
       "to": 15,
       "data":[
            {
                // Result Object
            },
            {
                // Result Object
            }
       ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-27
      • 2018-01-16
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      相关资源
      最近更新 更多