Losing the ability to pass seed
to the shuffle()
method in the collection helper
#54611
Unanswered
misbahansori
asked this question in
Ideas
Replies: 1 comment
-
I was annoyed with this change as well. As a fix, I'm using the following macros: Arr::macro('shuffleWithSeed', function (array $array, ?int $seed = null): array {
if (is_null($seed)) {
shuffle($array);
} else {
mt_srand($seed);
shuffle($array);
mt_srand();
}
return $array;
});
Collection::macro('shuffleWithSeed', function (?int $seed = null): Collection {
return new Collection(Arr::shuffleWithSeed($this->items, $seed));
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Laravel Version
^11.0
PHP Version
^8.1.0
Database Driver & Version
No response
Description
In the previous version of Laravel, we could pass a seed to the
shuffle()
method in the collection helper:https://laravel.com/api/10.x/Illuminate/Support/Collection.html#method_shuffle
However, in version 11, this method doesn't accept any arguments.
https://laravel.com/api/11.x/Illuminate/Support/Collection.html#method_shuffle
Steps To Reproduce
Expected behavior
The result should be the same given the same seed.
Current behavior
The result is different every time the code is executed.
Beta Was this translation helpful? Give feedback.
All reactions