2 years ago

Generate dummy data with Faker while still maintaining consistent data and relationships

Posted 2 years ago by lmartins

Im trying to generate a few records in a table that relates to two foreign keys, as shown bellow:

$factory->define(App\Assessment::class, function (Faker $faker) {
    return [
        'name' => $faker->name(),
        'email' => $faker->email(),
        'organization_id' => $faker->randomElement([1, 2]),
        'purchase_id' => $faker->numberBetween(1, 6),

Problem with this approach is that Purchases are also related to the Organization, so with the approach above we can end up with invalid combinations of organization and purchase, in other words pointing to a purchase which does not belong to the organization set randomly.

How would you typically approach this?

Please sign in or create an account to participate in this conversation.