Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Suggestion - Faker method to randomly execute #2046

@pnoeric

Description

@pnoeric

I'm using Faker with Laravel to populate a field that 70% of the time should have a random date, and the rest of the time it should be null. Here's how I do it now:

// 70% of the time set a date, otherwise it's null
'LastNudgeEmail' => (mt_rand(1, 10) > 3) ? $faker->dateTimeBetween("now", "+1 month") : null

I was thinking that it'd be cool if Faker had a new chainable method that would only pass along the chain if a % threshold was met; if it didn't pass, Faker would just return the value specified in the method call. So the above code could become a little more elegant and readable:

// 70% of the time set a date, otherwise it's null
'LastNudgeEmail' => $faker->chanceOfPassing(70, null)->dateTimeBetween("now", "+1 month")

(There's probably even a more elegant name/way to set up the method and its parameters... but you get the drift...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions