我是Laravel的新手,在使用keyBy()时无法进行分页。
以下作品:
$customers = Customers::paginate(10)
但是以下内容会导致错误:调用未定义的方法Illuminate \ Database \ Query \ Builder :: keyBy()
$customers = Customers::keyBy('id')->paginate(10)
那么在使用keyBy时如何分页?
我尝试将其插入AppServiceProvider.php的启动方法中,但无效:
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate',
function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator(
$this->forPage($page, $perPage), $this->count(), $perPage, $page,$options))->withPath('');
});
}