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
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

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

Rector

The latest

View all →
Sort Elements with the Alpine.js Sort Plugin image

Sort Elements with the Alpine.js Sort Plugin

Read article
Anonymous Event Broadcasting in Laravel 11.5 image

Anonymous Event Broadcasting in Laravel 11.5

Read article
Microsoft Clarity Integration for Laravel image

Microsoft Clarity Integration for Laravel

Read article
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article