Laravel User
Laravel User

Reputation: 1129

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

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 500 000 records. but it only returns maximum 500 records at a time. I can set the value to a lower than 500 but not more than 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.

Api Call returns:

Total results found - 456 789

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
                    )

            )

    )

Upvotes: 2

Views: 4211

Answers (1)

Blake Mark
Blake Mark

Reputation: 58

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;

Upvotes: 4

Related Questions