Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DanB's avatar

Collection::shuffle() seed parameter removed?

According to the API documentation, the seed parameter of the Illuminate\Support\Collection shuffle method was silently removed in 11.x (I don't see any mention in release notes or upgrade guide). This has broken my code that needs to be able to produce the same shuffle twice. Fundamentally changing functionality without notice is a foul! Was this an accident or a permanent change?

0 likes
4 replies
JussiMannisto's avatar

Looks like there was a recent PR for the upgrade guide about this, but it was rejected because the workaround suggestions in it were insecure.

Check the PR for the suggestions if you think it works for you and isn't a security concern.

DanB's avatar

Insecure? Randomizing a collection? Since randomizers have included the ability to use seeds since the beginning of time, and they're all inherently seeded internally, of course, it seems ridiculously presumptuous to feel the need to protect developers from themselves in this case. If a developer is using shuffling a collection as a security measure, their app probably has much bigger problems!

But anyway, that code will work for me. Thanks.

DanB's avatar

To anyone that implements this workaround, although it's almost certainly unnecessary, I think we should probably do something like this to help guarantee we don't leave the randomizer seeded with our value. That COULD be a big security issue.

try {
	mt_srand($seed);
	shuffle($array);
} finally {
	mt_srand();
}
1 like
DanB's avatar

Also note that in my case I need to also include the following to keep my Eloquent collection from being converted to a plain Laravel collection.

use Illuminate\Database\Eloquent\Collection as EloquentCollection;

EloquentCollection::macro('shuffleWithSeed', function (?int $seed = null): EloquentCollection {
	return new EloquentCollection(Arr::shuffleWithSeed($this->items, $seed));
}

Please or to participate in this conversation.