问题描述
Post::where('active',1)->paginate(10)
,我想获取集合当前部分的开始和结束位置。 我的意思是,如果页面是2,页面限制是10,则开始位置是11,结束位置是20。我需要渲染像284个产品中从11个到20个产品的文本。有可能吗?
解决方法
以下方法将返回您想要的信息:
$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将包含元信息,例如
total
,current_page
,last_page
等。实际结果对象 可通过JSON数组中的数据键使用。这是一个 通过从中返回分页器实例创建的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
}
]
}