Laravel 8.x Cursor Based Pagination Example

Good news for all Laravel devs that the Laravel team released 8.41 with cursor pagination, a new eloquent method to update models quietly, a new string method, and the latest changes in the 8.x branch:

There is no pagination for Laravel cursor. So to solve that issues Laravel team released 8.41 with cursor pagination. See the below example that how we can create cursor pagination. This support from Paras Malhotra contributed cursor pagination support. 

Example

$users = User::orderBy('id')->cursorPaginate(10);

 

Given the above pagination call for ten records, here’s an example of the response if we were to return this instance in a controller:

{
  "data": [
    {
      "id": 1,
      "name": "Nona Wilkinson",
      "email": "stephen68@example.com",
      "email_verified_at": "2021-05-12T23:21:19.000000Z",
      "created_at": "2021-05-12T23:21:19.000000Z",
      "updated_at": "2021-05-12T23:21:19.000000Z"
    },
    {
      "id": 2,
      "name": "Titus Feeney Sr.",
      "email": "oklein@example.com",
      "email_verified_at": "2021-05-12T23:21:19.000000Z",
      "created_at": "2021-05-12T23:21:19.000000Z",
      "updated_at": "2021-05-12T23:21:19.000000Z"
    },
    {...}
  ],
  "path": "http://127.0.0.1:8000/users",
  "per_page": 10,
  "next_page_url": "http://127.0.0.1:8000/users?cursor=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0",
  "prev_page_url": null
}

 

Read also: Use Cursor in Eloquent Query to Reduce Memory in Laravel

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js