Laravel 分页不适用于数组而不是集合 [英] Laravel pagination not working with array instead of collection

查看:28
本文介绍了Laravel 分页不适用于数组而不是集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对数组数据集进行分页,事实证明它比我想象的更具挑战性.

I'm trying to paginate an array data set and it has proven more challenging than I thought.

我使用的是 Laravel 5

I'm using Laravel 5

所以我有一个抽象接口/存储库,我的所有其他模型都扩展到它,我在我的抽象存储库调用 paginate 中创建了一个方法.我已经包括了两个

So I have an abstract interface/repository that all my other models extend to and I created a method inside my abstract repository call paginate. I've included both

使用IlluminatePaginationPaginator;

使用 IlluminatePaginationLengthAwarePaginator;

方法在这里

  public function paginate($items,$perPage,$pageStart=1)
    {

        // Start displaying items from this number;
        $offSet = ($pageStart * $perPage) - $perPage; 

        // Get only the items you need using array_slice
        $itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);

        return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
    }

所以你可以想象这个函数接受一个 $items 数组和一个 $perPage 变量,它指示要分页的项目数量和一个 $pageStart 表示从哪个页面开始.

So as you can imagine this function accepts an array of $items a $perPage variable that indicates how many to items to paginate and a $pageStart that indicates from which page to start.

分页有效,当我执行 dd() 时,我可以看到 LengthAwarePaginator 实例,它的所有值看起来都很好.

The pagination works and I can see LengthAwarePaginator instance when I'm doing a dd() , all of it's values seem fine.

当我显示结果时问题就开始了.

The problem starts when I'm displaying the results.

当我做 {!!$instances->render() !!} 分页器链接显示正常,page 参数根据链接变化,但数据没有变化.每一页的数据都是一样的.例如,当我使用 Eloquent 时 Model::paginate(3) 一切正常,但是当我 dd() 这个 LengthAwarePaginator 它是相同的到我的自定义分页器的 LengthAwarePaginator 实例,除了它对数组进行分页,而不是对集合进行分页.

When I do {!! $instances->render() !!} The paginator links display's fine, the page parameter changes according to links but the data is not changing. Data is the same in every page. When I'm using Eloquent for example Model::paginate(3) everything works fine, but when I dd() this LengthAwarePaginator it's identical to the LengthAwarePaginator instance of my custom paginator with the exception that it paginates an array ofcourse and not a collection.

推荐答案

你没有传递当前页面,就像你应该的那样,所以你也得到了相同的数组.这会起作用

You are not passing the current page, like you should so you also get same array. This will work

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

    // Get only the items you need using array_slice
    $itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);

    return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}

如果您为 $pageStart - Request::get('page', 1)

这篇关于Laravel 分页不适用于数组而不是集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆