Highly Performant Cursor Pagination in Laravel 8.41

News

May 13th, 2021

cursor-paginate.jpg

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:

Cursor Pagination

Paras Malhotra contributed cursor pagination support. The Laravel Documentation has an excellent explanation of what separates cursor pagination from other pagination methods used to retrieve pages of data from the database:

Cursor pagination works by constructing “where” clauses that compare the values of the ordered columns contained in the query, providing the most efficient database performance available amongst all of Laravel’s pagination methods. This method of pagination is particularly well-suited for large data-sets and “infinite” scrolling user interfaces.

Unlike offset based pagination, which includes a page number in the query string of the URLs generated by the paginator, cursor based pagination places a “cursor” string in the query string.

Cursor pagination solves duplication issues found in offset pagination, which typically impacts infinite scroll and API usage. Cursor pagination can be more efficient with big data sets.

Here’s a quick example of what it looks like:

1$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:

1{
2 "data": [
3 {
4 "id": 1,
5 "name": "Nona Wilkinson",
6 "email": "stephen68@example.com",
7 "email_verified_at": "2021-05-12T23:21:19.000000Z",
8 "created_at": "2021-05-12T23:21:19.000000Z",
9 "updated_at": "2021-05-12T23:21:19.000000Z"
10 },
11 {
12 "id": 2,
13 "name": "Titus Feeney Sr.",
14 "email": "oklein@example.com",
15 "email_verified_at": "2021-05-12T23:21:19.000000Z",
16 "created_at": "2021-05-12T23:21:19.000000Z",
17 "updated_at": "2021-05-12T23:21:19.000000Z"
18 },
19 {...}
20 ],
21 "path": "http://127.0.0.1:8000/users",
22 "per_page": 10,
23 "next_page_url": "http://127.0.0.1:8000/users?cursor=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0",
24 "prev_page_url": null
25}

As you can see, you cannot rely on the ?page=2 query param used for offset pagination. You must use the provided next_page_url param to get the next page of results.

Cursor pagination provides a massive benefit to the Laravel framework and ecosystem—congratulations to Paras Malhotra for this fantastic contribution (and his many other contributions).

Update a Model Quietly

GitHub user @YTauber contributed an Eloquent method to update a model without raising any events:

1// `updateQuietly` works like `update()` but without raising events
2
3$user->updateQuietly(['email' => 'user@example.com']);

Str Replace Method

Selçuk Çukur contributed a Str::replace() method for use with the Str and Stringable classes:

1use Illuminate\Support\Str;
2
3Str::replace('dead', 'alive', 'PHP is dead');
4/* PHP is alive */
5
6Str::of('PHP is dead')->replace('dead', 'alive');
7/*
8Illuminate\Support\Stringable {#4091
9 value: "PHP is alive",
10}
11*/

Release Notes

You can see the full list of new features and updates below and the diff between 8.40.0 and 8.41.0 on GitHub. The following release notes are directly from the changelog:

v8.41.0

Added

  • Added Illuminate\Database\Eloquent\Model::updateQuietly() (#37169)
  • Added Illuminate\Support\Str::replace() (#37186)
  • Added Model key extraction to id on whereKey() and whereKeyNot() (#37184)
  • Added support for Pusher 6.x (#37223, 819db15)
  • Added Illuminate/Foundation/Http/Kernel::getMiddlewarePriority() (#37271)
  • Added cursor pagination (aka keyset pagination) (#37216, #37315)
  • Support mass assignment to SQL Server views (#37307)
  • Added Illuminate/Support/Stringable::unless() (#37326)

Fixed

  • Fixed Illuminate\Database\Query\Builder::offset() with non numbers $value (#37164)
  • Treat missing UUID in failed Queue Job as empty string (failed driver = database) (#37251)
  • Fixed fields not required with required_unless (#37262)
  • SqlServer Grammar: Bugfixes for hasTable and dropIfExists / support for using schema names in these functions (#37280)
  • Fix PostgreSQL dump and load for Windows (#37320)

Changed

  • Add fallback when migration is not anonymous class (#37166)
  • Ably expects clientId as string in Illuminate\Broadcasting\Broadcasters\AblyBroadcaster::validAuthenticationResponse() (#37249)
  • Computing controller middleware before getting excluding middleware (#37259)
  • Update mime extension check (#37332)
  • Added exception to chunkById() when last id cannot be determined (#37294)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.