手动创建分页器 (Laravel 5)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29240758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manually Creating a Paginator (Laravel 5)
提问by Sturm
I seem to be unable to manually create an instance of the paginator.
我似乎无法手动创建分页器的实例。
use Illuminate\Pagination\Paginator;
class Blah {
public function index(Paginator $paginator)
{
// Build array
$var = $paginator->make($array, $count, 200);
return $var;
}
}
From here I'm just getting Unresolvable dependency resolving [Parameter #0 [ <required> $items ]] in class Illuminate\Pagination\Paginator
从这里我刚刚得到 Unresolvable dependency resolving [Parameter #0 [ <required> $items ]] in class Illuminate\Pagination\Paginator
回答by mirzap
There is no more make() method in laravel 5. You need to create an instance of either an Illuminate\Pagination\Paginator
or Illuminate\Pagination\LengthAwarePaginator
. Take a look at documentation page, Creating A Paginator Manuallypart
Laravel 5 中不再有 make() 方法。您需要创建 anIlluminate\Pagination\Paginator
或的实例Illuminate\Pagination\LengthAwarePaginator
。看一下文档页面,手动创建分页器部分
http://laravel.com/docs/master/pagination
http://laravel.com/docs/master/pagination
I guess it'll look something like this:
我想它看起来像这样:
use Illuminate\Pagination\Paginator;
class Blah {
public function index()
{
// Build array
$array = [];
return new Paginator($array, $perPage);;
}
}
Also check thisanswer.
也检查这个答案。
回答by Amin Shojaei
In 2019, to manualy paginate items in laravel you should instantiate Illuminate\Pagination\Paginator class like this:
在 2019 年,要在 laravel 中手动分页项目,您应该像这样实例化 Illuminate\Pagination\Paginator 类:
// array of items
$items = [ 'a' => 'ana', 'b' => 'bla', 'c' => 'cili' ];
// items per page
$perPage = 2;
// let paginator to regonize page number automaticly
$currentPage = null;
// create paginator instance
$paginate = new Paginator($items, $perPage);
hope this helps.
希望这可以帮助。