【问题标题】:Manually Creating A Paginator in Laravel 5.1在 Laravel 5.1 中手动创建分页器
【发布时间】:2016-03-18 11:07:05
【问题描述】:

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

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()]);

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

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

有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-05-28
        • 2015-10-30
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        • 2015-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多