Skip to content

[13.x] Fix chunkById() and lazyById() looping forever on models with a cast primary key - #61816

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:chunk-by-id-cast-key
Oct 2, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:chunk-by-id-cast-key

Conversation

@xurshudyan

Copy link
Copy Markdown
Contributor

When a model casts its key, chunkById() and lazyById() read the last ID of each page through the cast. That means the value bound into the next where id > ? query is the cast value, not the one stored in the database. With AsBinary::uuid(), the next page binds a 36 character UUID string against a binary(16) column, gets the same rows back, and the loop never ends.

For example, with 'id' => AsBinary::uuid() (the example from #58254), Model::chunkById(100, ...) keeps returning the first page with the same binding. The same happens in eachById(), lazyById(), and model:prune, since prunable models are chunked by ID too.

This PR reads the raw attribute when the result is a model, so the next page is bound with the same value the database returned. Plain query builder results still go through data_get() as before.

@xurshudyan
xurshudyan force-pushed the chunk-by-id-cast-key branch from 5dc0a84 to 3bab133 Compare October 2, 2026 05:24
@taylorotwell
taylorotwell merged commit e2c81f5 into laravel:13.x Oct 2, 2026
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants