Skip to content

[13.x] Fix collapseWithKeys() crashing when the outer collection has string keys - #61539

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
rayblair06:13.x-collapse-with-keys-string-keys
Sep 11, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
rayblair06:13.x-collapse-with-keys-string-keys

Conversation

@rayblair06

@rayblair06 rayblair06 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Collection::collapseWithKeys() throws when the outer collection has string keys, because the nested arrays are unpacked into array_replace() with their original keys, and PHP treats string keys in an unpacked array as named arguments:

collect([
    'mail' => ['mail.driver' => 'smtp', 'mail.port' => 587],
    'queue' => ['queue.default' => 'redis'],
])->collapseWithKeys();

// ArgumentCountError: array_replace() expects at least 1 argument, 0 given

Mixing integer and string keys fails too, with array_replace() does not accept unknown named parameters.

Since the outer keys get thrown away anyway, I've changed it to collect the nested arrays into a plain list before passing them to array_replace(). That's what Arr::collapse() already does. Collections with integer keys behave exactly as before.

LazyCollection::collapseWithKeys() already handles string keys, so the new test runs against both implementations to keep them consistent.

@taylorotwell
taylorotwell merged commit ecff444 into laravel:13.x Sep 11, 2026
56 checks passed
binaryfire added a commit to hypervel/components-backup that referenced this pull request Oct 1, 2026
Handle string outer keys in collapseWithKeys and describe receiver types after collection mutation, including nullable keys. Return base collections for Eloquent duplicate attribute or callback values; keep model collections when no callback is supplied.

Correct mapped duplicate and sum types, annotate empty builders and Wayfinder defaults, and preserve explicit null-key behavior without PHP 8.5 deprecations. Consolidate overlapping coverage. Collection tests and full type analysis pass.

Upstream:
laravel/framework#61539
laravel/framework#61571
laravel/framework#61572
laravel/framework#61578
laravel/framework#61597
laravel/framework#61611
laravel/framework#61621

Source: laravel/framework at 7068848dfe48fc3a433598e09ce798799d442a52.
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