Skip to content

[12.x] Allow $preserveKeys param for LazyCollection random#58791

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
jackbayliss:12.x-allow-preversekeys-for-lazy-collection
Feb 13, 2026
Merged

[12.x] Allow $preserveKeys param for LazyCollection random#58791
taylorotwell merged 1 commit intolaravel:12.xfrom
jackbayliss:12.x-allow-preversekeys-for-lazy-collection

Conversation

@jackbayliss
Copy link
Contributor

@jackbayliss jackbayliss commented Feb 12, 2026

Collection::random() already allows the preserveKeys param

LazyCollection passes through to the collection.. this just allows us to specify the preserveKey param which defaults to false.

this just matches the Collection API version of random.. for consistency

$entries = LazyCollection::make([
    'entry_9821' => ['name' => 'BigT', 'email' => 'bigt@laravel.com'],
    'entry_9393' => ['name' => 'Bob', 'email' => 'bob@example.com'],
    'entry_3108' => ['name' => 'Charlie', 'email' => 'charlie@example.com'],
]); 

$winners = $entries->random(2, true);
// Result: ['entry_9821' => [...], 'entry_3108' => [...]]

I couldn't see a test, so added one

Thanks

@github-actions
Copy link

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss jackbayliss marked this pull request as ready for review February 12, 2026 23:20
@taylorotwell taylorotwell merged commit c50f17a into laravel:12.x Feb 13, 2026
72 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