Collection::shuffle
Returns a new collection with the same items in a randomized order.
Collection::shuffle(): staticReturn value
staticA new collection containing every original item in a random order, reindexed from zero.
Changelog
- PHP 11.xThe $seed parameter was removed. Shuffling now goes through PHP's Random\Randomizer and can no longer be made reproducible by passing a seed.
Every item is kept; only the order changes. Unlike most collection methods, keys are discarded — the result is always reindexed as a list, because an order-randomizing operation on a keyed map would be meaningless. If your collection is keyed by id and you need those ids afterwards, shuffle the keys instead, or use random($count, true).
Reach for it when the whole set is needed in an arbitrary order: dealing a deck, rotating banner slots per request, randomizing answer options for a quiz. If you only want a few items, random($n) is both clearer and cheaper than shuffling and slicing. For a large database table, inRandomOrder() in the query avoids hydrating every row first.
You cannot seed it
Laravel 11 removed the $seed argument; shuffling now uses PHP's Random\Randomizer, which is seeded from the OS. There is no supported way to make the output reproducible, so tests must assert on properties — the count is unchanged, the sorted contents match, no item was lost — rather than on a specific order. Snapshot-testing a shuffled result is a flaky test waiting to happen.
Example
When to use it
- Deal a randomized hand from a deck of cards in a game loop.
- Randomize the order of multiple-choice answers so the correct one is not always first.
- Rotate sponsor logos or testimonials so no single one always appears at the top.
Frequently asked questions
How do I seed shuffle so tests are repeatable?
You cannot — Laravel 11 removed the $seed parameter and shuffling now uses PHP's Random\Randomizer. Test the invariants instead: the count, and that the contents match ignoring order.
Does shuffle keep my array keys?
No, it always reindexes the result from zero, unlike almost every other collection method. Shuffle keys() separately if you need to keep an id-based mapping.
Should I use shuffle or random to pick a few items?
Use random($n) when you only want a few items — it draws them directly instead of reordering the entire collection first. shuffle() is for when you need every item in an arbitrary order.
More examples
Keys are not preserved
The first dump is bool(true): the id keys are gone after a plain shuffle. Shuffling keys() instead gives you a randomized list of ids you can still use for lookups; the assertions print int(3) and bool(true) on every run.
<?php
$byId = collect([
17 => 'Desk',
42 => 'Chair',
99 => 'Lamp',
]);
var_dump($byId->shuffle()->keys()->all() === [0, 1, 2]);
// Keep the ids by shuffling the keys and mapping back.
$order = $byId->keys()->shuffle();
var_dump($order->count());
var_dump($order->sort()->values()->all() === [17, 42, 99]);Testing code that shuffles
assertEqualsCanonicalizing compares contents while ignoring order, which is the only stable assertion available once the seed argument is gone. Asserting a literal order here would pass roughly one run in twenty-four.
<?php
namespace Tests\Unit;
use App\Services\QuizBuilder;
use Tests\TestCase;
class QuizBuilderTest extends TestCase
{
public function test_answers_are_randomized_but_complete(): void
{
$answers = ['a', 'b', 'c', 'd'];
$shuffled = (new QuizBuilder)->options($answers);
// Assert on properties, never on a fixed order.
$this->assertCount(4, $shuffled);
$this->assertEqualsCanonicalizing($answers, $shuffled->all());
}
}Shuffle versus random versus inRandomOrder
All three produce randomized output but at very different costs. shuffle() and random() both require the full result set in memory; inRandomOrder() pushes the work into SQL and is the only one that scales past a few thousand rows.
<?php
namespace App\Http\Controllers;
use App\Models\Sponsor;
class SponsorController extends Controller
{
public function index()
{
// Small set already in memory: shuffle the lot.
$rotated = Sponsor::active()->get()->shuffle();
// Only need three: random() is cheaper than shuffle()->take(3).
$spotlight = Sponsor::active()->get()->random(3);
// Large table: let the database do it, never hydrate every row.
$sampled = Sponsor::inRandomOrder()->limit(3)->get();
return view('sponsors', compact('rotated', 'spotlight', 'sampled'));
}
}
Discussion