Laravel Paginator ::使构造不正确

时间:2014-03-19 16:33:14

标签: php laravel laravel-4

我正在使用以下代码创建一个分页器:

    $postsPerPage = 10;

    $posts = Post::where('published', '=', true)->get()->toArray();

    $totalItems = count($posts);

    $posts = Helpers::transform_posts($posts);

    $posts = Paginator::make($posts, $totalItems , $postsPerPage);

    return $posts;

但它错误地构建了它。在"数据"中返回12项(帖子总数)阵列。

{
    "total": 12,
    "per_page": 10,
    "current_page": 1,
    "last_page": 2,
    "from": 1,
    "to": 10,
    "data": [
        // lists 12 items
    ]
}

有人能发现我做错了吗?

为了完整性'为了这里的transform_posts()函数:

public static function transform_posts(array $posts)
{
    foreach ($posts as &$array) {
        $array = Helpers::transform_post($array);
    }
    return $posts;
}

public static function transform_post(array $post)
{
    $publishedDate = \Carbon\Carbon::createFromTimeStamp(strtotime($post['published_date']))->diffForHumans();

    return [
        'id' => $post['id'],
        'title' => $post['title'],
        'slug' => $post['slug'],
        'subtitle' => $post['subtitle'],
        'body' => $post['body'],
        'published' => (bool)$post['published'],
        'published_date' => $post['published_date'],
        'published_date_for_humans' => $publishedDate
    ];
}

1 个答案:

答案 0 :(得分:0)

由于您手动分页,因此您必须自己对$posts数组进行切片。