如果我使用Something::paginate()
,我将在Laravel 5中每页获得15个项目。课程我可以随时进行Something::paginate(20)
。
但是如何覆盖我的.env中的默认计数和使用值?
答案 0 :(得分:7)
您可以通过添加
覆盖模型中的$ perPage变量 protected $perPage = 10;
在模型中覆盖Model.php中定义的$ perPage = 15原始变量
答案 1 :(得分:2)
这个问题是很久以前才提出的,但是如果有人需要一种解决方法,则可以在模型中使用特征。我必须从请求中获取per_page才能接受“全部”并返回所有记录,且最大值不能超过。
<?php
namespace App\Traits;
trait Paginatable
{
protected $perPageMax = 1000;
/**
* Get the number of models to return per page.
*
* @return int
*/
public function getPerPage(): int
{
$perPage = request('per_page', $this->perPage);
if ($perPage === 'all') {
$perPage = $this->count();
}
return max(1, min($this->perPageMax, (int) $perPage));
}
/**
* @param int $perPageMax
*/
public function setPerPageMax(int $perPageMax): void
{
$this->perPageMax = $perPageMax;
}
}
希望有帮助...