Skip to content

[13.x] Throw ValueError in LazyCollection::combine() on mismatched lengths - #61696

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
mrjavadseydi:13.x-lazy-collection-combine-value-error
Sep 24, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
mrjavadseydi:13.x-lazy-collection-combine-value-error

Conversation

@mrjavadseydi

Copy link
Copy Markdown
Contributor

Collection::combine() uses array_combine(), which throws a ValueError when the keys and values have diffrent lengths. LazyCollection::combine() triggers an E_USER_WARNING instead, which matches how array_combine() behaved before PHP 8:

(new Collection([1, 2]))->combine([3]);            // ValueError
(new LazyCollection([1, 2]))->combine([3])->all(); // Warning, returns [1 => 3]

Inside a Laravel app the warning gets converted to an ErrorException, so the two classes throw different exceptions for the same input. When illuminate/collections is used on its own the warning may not be converted at all, and the call just returns truncated data without anyone noticing.

This throws a ValueError with the existing message so both classes fail the same way. Its still lazy, the error is only thrown if iteration actually reaches the mismatch, so something like combine()->take(1) on mismatched input behaves the same as before.

Added tests for both directions (fewer values than keys, and more values than keys) that run against both Collection and LazyCollection through the collection class provider. They fail on LazyCollection without the fix.

@mrjavadseydi
mrjavadseydi force-pushed the 13.x-lazy-collection-combine-value-error branch from c3d0bde to 0734c6e Compare September 23, 2026 22:27
@taylorotwell
taylorotwell merged commit d97d2e7 into laravel:13.x Sep 24, 2026
27 of 60 checks passed
@mrjavadseydi
mrjavadseydi deleted the 13.x-lazy-collection-combine-value-error branch September 24, 2026 19:41
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