Thomas Charlesworth
Thomas Charlesworth

Reputation: 1839

laravel 5 paginating json data set

Controller Code: http://laravel.io/bin/1yYrw
View Code: http://laravel.io/bin/kWlaX

I'm currently pulling a set of data from a guzzle api request. My variable $transactions on line 28 returns this data set:

[
   {
      "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"
   }
]

However, when paginated and displayed in the view using a foreach loop, all 11 items are shown instead of being paginated. When I changed the perpage value from 6 to 2, the $->render() slider seems to understand that more pages are produced but all items are still shown on the same page.

Upvotes: 0

Views: 955

Answers (2)

Snarcraft
Snarcraft

Reputation: 21

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

Upvotes: 0

EspadaV8
EspadaV8

Reputation: 668

You're calling the foreach on the paginate class which implements an ArrayIterator [1] over all the items. I think what you probably way to do is use the forPage [2] method on the collection itself (any method that doesn't exist on the Paginator gets passed to the Collection [3]), passing in the page number and number per page you want.

@foreach ($paginator->forPage($paginator->currentPage(), $paginator->perPage())
    ...
@endforeach

[1] https://github.com/illuminate/pagination/blob/master/AbstractPaginator.php#L374
[2] https://github.com/illuminate/support/blob/master/Collection.php#L413
[3] https://github.com/illuminate/pagination/blob/master/AbstractPaginator.php#L461

Upvotes: 2

Related Questions