Laravel Idea for PhpStorm - Full-featured IDE for productive artisans!

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.

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

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant 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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Asymmetric Property Visibility in PHP 8.4 image

Asymmetric Property Visibility in PHP 8.4

Read article
Access Laravel Pulse Data as a JSON API image

Access Laravel Pulse Data as a JSON API

Read article
Laravel Forge adds Statamic Integration image

Laravel Forge adds Statamic Integration

Read article
Transform Data into Type-safe DTOs with this PHP Package image

Transform Data into Type-safe DTOs with this PHP Package

Read article
PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell image

PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell

Read article
Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3 image

Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3

Read article