Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

[5.4] Collection::random(int $amount) should always return a collection #304

@sebastiandedeyne

Description

@sebastiandedeyne

Currently, if you call $collection->random(1), you'll get a single item back. This makes sense if there isn't a parameter set, but if 1 is explicitly passed through, it should return a new collection instance. This keeps it consistent with passing in any other integer.

Example use case: if you want to retrieve a random number of random items (useful in seeders), you can't predict the return value:

$collection->random(rand(0, 5));
// Should return a collection containing 0 to 5 items

Proposed solution: change the default value to null, so $collection->random() still returns a single item.

If there's interest I'm willing to spin up a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions