Highly Performant Cursor Pagination in Laravel 8.41

News

May 13th, 2021

Highly Performant Cursor Pagination in Laravel 8.41

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:

$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
}

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:

// `updateQuietly` works like `update()` but without raising events
 
$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:

use Illuminate\Support\Str;
 
Str::replace('dead', 'alive', 'PHP is dead');
/* PHP is alive */
 
Str::of('PHP is dead')->replace('dead', 'alive');
/*
Illuminate\Support\Stringable {#4091
value: "PHP is alive",
}
*/

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.