在Laravel 5.1中手动创建一个Paginator

时间:2015-12-12 16:21:27

标签: php laravel pagination laravel-5 laravel-5.1

我尝试手动创建分页实例,如下所示

use Illuminate\Pagination\LengthAwarePaginator as Paginator;

 public function createPaginate()
 {

  $page =1;  // Get the current page or default to 1
  $perPage = 2;
  //this is my array 
  $items = $this->getCurrentServices($this->getServices(new MaintenanceService, 'maintenance_service')->get());

  $offset = ($page * $perPage) - $perPage;

  $maintenanceServices = new Paginator (
                         array_slice($items, $offset,  $perPage, true),
                         count($items),$perPage,Paginator::resolveCurrentPage(), 
                         array('path' =>  Paginator::resolveCurrentPath())
                        );

  return view('my-view', compact('maintenanceServices'));

以上代码在my-view中运行良好,但在页面之间切换时数据不会改变

此外,我尝试使用Request更改我的代码以检查网址

 $maintenanceServices =new Paginator (
                       array_slice($items, $offset, $perPage, true),
                       count($items), $perPage, $page, 
                       ['path' => $request->url(), 'query' => $request->query()]);

最后我使用$items检查dd($items),结果是

array:7 [▼
0 => MaintenanceService {#245 ▶}
1 => MaintenanceService {#246 ▶}
2 => MaintenanceService {#247 ▶}
3 => MaintenanceService {#248 ▶}
4 => MaintenanceService {#249 ▶}
5 => MaintenanceService {#250 ▶}
6 => MaintenanceService {#251 ▶}
     ]

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

在laravel 5.1中,您可以执行以下操作

$pageStart = request()->get('page', 1);
$offset = ($pageStart * $perPage) - $perPage;

答案 1 :(得分:0)

我找到了解决方案here  我没有通过当前页面。

$pageStart = \Request::get('page', 1);
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage;