php - 在 Laravel 5.1 中手动创建分页器

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

最后我使用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 ▶}
     ]

有什么建议吗?

最佳答案

在 Laravel 5.1 中你可以执行以下操作

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

关于php - 在 Laravel 5.1 中手动创建分页器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242121/

相关文章:

javascript - 将 Node.js 与 Symfony2 或 PHP 集成

php - sql查询重叠时间

php - 获取一周前/期间/一周后的生日

php - 注册表单上的 Laravel 自定义验证

php - Laravel 没有将模型加载到路由中

cakephp - 自定义 cakephp 分页以使用 Solr 查询

javascript - 上传数据到网站

php - Laravel 解决闭包的依赖关系

php - 通过分页获取结果总数

c# - 如何在没有数据源的情况下构建分页 GridView