Dave Wize's avatar

Paginate with zero index

Hello. I want to get my paginator to index from zero because I'm building an API that is used within a third-party system that starts paginating from zero. Is there a way to make a specific paginator zer indexed?

0 likes
10 replies
LaryAI's avatar
Level 58

Yes, you can achieve this by customizing the Paginator class in Laravel. Here's how you can do it:

  1. Create a new class that extends the Illuminate\Pagination\Paginator class:
<?php

namespace App\Pagination;

use Illuminate\Pagination\Paginator;

class ZeroIndexPaginator extends Paginator
{
    /**
     * Get the current page for the request.
     *
     * @param  string  $pageName
     * @param  int|null  $default
     * @return int
     */
    public function currentPage($pageName = 'page', $default = null)
    {
        $page = parent::currentPage($pageName, $default);

        return $page - 1;
    }
}
  1. In your controller, use the ZeroIndexPaginator class instead of the default Paginator class:
use App\Pagination\ZeroIndexPaginator;

...

$items = MyModel::paginate(10, ['*'], 'page', $page);
$paginator = new ZeroIndexPaginator($items, 10, $page);

This will create a paginator that starts from zero instead of one.

Dave Wize's avatar

@LaryAI I don't understand how to apply the zeroIndexPaginator class to my eloquent model?

jlrdw's avatar

@Dave Wize The first offset is always 0, if that is what you mean.

$offset = ($page - 1) * $perpage;

If 5 per page:

$offset = (1 - 1) * 5; // 0

// Next offset

$offset = (2 - 1) * 5;  // 5

etc...
1 like
Dave Wize's avatar

@jlrdw No.

I need the first page value to be page=0

I've been facing an issue with the third party I use to fetch data from the Laravel API. They begin indexing from 0, assuming that the second page is page=1. So, when they make the second request, which is example.com/api/posts?page=1, it returns the same data as before.

I hope I expressed myself clearly enough

Thanks for your help

jlrdw's avatar

@Dave Wize the api will have the basic paginating instructions, can you post just that portion here.

1 like
Dave Wize's avatar

@jlrdw This is the index method for that API call

       return
        AccountResource::collection(
            AccountMaster::search($request->input('search'))->paginate(10)
        );
jlrdw's avatar

@Dave Wize That's not the instructions, at least show what is returned, short version, you don't have to put 50 records. You are probably getting json returned.

This

AccountMaster::search($request->input('search'))->paginate(10)

Looks like normal laravel pagination is used.

Edit:

Try the A.I. answer, you have to make the class and call it.

1 like
Dave Wize's avatar

@jlrdw Here is the results.

I'm using regular Laravel pagination

{
    "data": [
        {
            "id": 378090,
            "fullName": "Chaim Zeidenfeld",
            "firstName": " ",
            "benReb": "",
            "lastName": "",
            "maidenName": "",
            "locations": [
                {
                    "location": "Williamsburg",
                    "address": "176 Ross St  Brooklyn New York 11211 US",
                    "telephone": "845-2747920"
                }
            ],
            "business": true
        }
    ],
    "links": {
        "first": "http://159.223.140.6/api/account?page=1",
        "last": "http://159.223.140.6/api/account?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "links": [
            {
                "url": null,
                "label": "&laquo; Previous",
                "active": false
            },
            {
                "url": "http://159.223.140.6/api/account?page=1",
                "label": "1",
                "active": true
            },
            {
                "url": null,
                "label": "Next &raquo;",
                "active": false
            }
        ],
        "path": "http://159.223.140.6/api/account",
        "per_page": 10,
        "to": 1,
        "total": 1
    }
}
jlrdw's avatar

@Dave Wize

I'm building an API that is used within a third-party system that starts paginating from zero.

Is the third party system an API?

If so they should have instructions.

Are you writing an API to serve another API? I admit, I am not sure what is going on. Why would the third party system require a page 0?

If that is the case, write a class and use the 0 for one. You need a custom class as suggested.

1 like
Dave Wize's avatar
Dave Wize
OP
Best Answer
Level 18

@jlrdw This is a Flutterflow app built by my client, and I'm doing the API for it.

In order to enable infinite pagination, the system queries the next set of data on scroll. Flutterflow has a built-in nextPagePagination variable that increments with each request. However, since it starts at zero, the second API call requested page=1 which resulted in duplicate data being returned.

I was able to achieve it by using a custom FF code expression that incremented the nextPagePagination variable by 1.

Please or to participate in this conversation.