Highly Performant Cursor Pagination in Laravel 8.41

Published on by

Highly Performant Cursor Pagination in Laravel 8.41 image

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)
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project.

Visit No Compromises
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article
Use Google's Gemini AI in Laravel image

Use Google's Gemini AI in Laravel

Read article
PhpStorm is getting a brand new terminal image

PhpStorm is getting a brand new terminal

Read article
Six Essential Plugins for Visual Studio Code image

Six Essential Plugins for Visual Studio Code

Read article
Modularize Your Laravel Application With the Modular Package image

Modularize Your Laravel Application With the Modular Package

Read article