laravel 5分页json数据集

时间:2015-04-22 04:08:08

标签: php json laravel pagination

控制器代码:http://laravel.io/bin/1yYrw
查看代码:http://laravel.io/bin/kWlaX

我正在从guzzle api请求中提取一组数据。第28行的变量$ transactions返回此数据集:

[
   {
      "game":"Fantasista",
      "package":"Fantasista 10 Dollca",
      "date":"2015-04-21T10:23:44.000Z"
   },
   {
      "game":"Fantasista",
      "package":"Fantasista 10 Dollca",
      "date":"2015-04-21T10:21:43.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"MOL Points Inter Wallet 500 MB Coins",
      "date":"2015-04-21T10:19:31.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"MOL Points Inter Wallet 500 MB Coins",
      "date":"2015-04-21T10:18:10.000Z"
   },
   {
      "game":"Fantasista",
      "package":"Fantasista 10 Dollca",
      "date":"2015-04-21T07:51:35.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"Thai ePay 500",
      "date":"2015-04-21T07:41:13.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"Thai ePay 1000",
      "date":"2015-04-21T07:38:08.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"Thai ePay 50",
      "date":"2015-04-21T07:35:50.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"Thai ePay 150",
      "date":"2015-04-21T07:23:24.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"Boonterm 150",
      "date":"2015-04-21T07:06:38.000Z"
   },
   {
      "game":"MagicBoxAsia",
      "package":"Thai ePay 50",
      "date":"2015-04-21T06:43:54.000Z"
   }
]

但是,当使用foreach循环对视图进行分页并显示时,将显示所有11个项目而不是分页。当我将perpage值从6更改为2时,$->render()滑块似乎理解生成了更多页面,但所有项目仍显示在同一页面上。

1 个答案:

答案 0 :(得分:0)

Do this

 $collection = new Collection($response['data']), true));

 $currentPage = Paginator::resolveCurrentPage();

 $per_page = 6;

 $currentPageResults = $collection->slice(($currentPage - 1) * $per_page, $per_page)->all();

 $paginator = new Paginator($currentPageResults, count($collection), $per_page);

 $paginator->setPath($request->url());