Laravel User Laravel User - 5 months ago 125
PHP Question

How to create custom pagination on API returned results in laravel 5.2?

PS: I know how to create pagination from database returned records in laravel view. but need to create the same from API returned results.

I have an api which have around 5 lac records. but it only return maximum 500 records at a time. i can set the value to a lower than 500 but not more then that in a single api call. i have total records returned by the api also. i want to create a pagination of 50 results per page. Can anyone provide the code for this in laravel 5.2 using blade template or jquery any help would be great.

Api Call returns:-

Total results found - 456789

Total records returned - 500 or less

Array
(
[response] => Array
(
[metaData] => Array
(
[resultsAvailable] => 228246
[resultsReturned] => 500
[firstResult] => 1
[lastResult] => 500

)

[results] => Array
(
[0] => Array
(
[0] => 100000
[1] => 1
[2] => 14327
[3] => 5
[4] => 3
[5] => 2014-02-18T07:40:23.000Z
[6] => 4
[7] => 390770731738-385691128026
[8] => 1
[9] => 2014-02-20T20:51:56.000Z
[10] => 0
)

[1] => Array
(
[0] => 100001
[1] => 1
[2] => 14328
[3] => 5
[4] => 3
[5] => 2014-02-18T08:20:56.000Z
[6] => 4
[7] => 026-2336690-3199543
[8] => 1
[9] => 2014-02-20T20:51:55.000Z
[10] => 0
)
.......



[499] => Array
(
[0] => 100511
[1] => 1
[2] => 14688
[3] => 5
[4] => 3
[5] => 2014-02-20T19:35:00.000Z
[6] => 4
[7] => 205-0866154-3716348
[8] => 1
[9] => 2014-02-20T20:46:45.000Z
[10] => 0
)

)

)

Answer

You need to add use:

use Illuminate\Pagination\LengthAwarePaginator as Paginator;

and now you can use:

$paginator = new Paginator($items, $count, $limit, $page, [
            'path'  => $this->request->url(),
            'query' => $this->request->query(),
        ]);

to get data in the same format as paginating on model object;