dan3460's avatar

Seeding Unique Pairs

I'm trying to seed a unique combination into a table. In the DatabaseSeeder i have the following:

$users = User::factory(10)->create();
$producers = Producer::factory(20)->recycle($countries)->recycle($companies)->create();
UserProducer::factory(300)->recycle($users)->recycle($producers)->create();

I have a table that maintain the relationship between users and producers (each user have some users). How do i call the factory so i recycle users but assign unique producers to them. I have the factory shown but that produces many repeated combinations. Is there an easy way to do this?

0 likes
1 reply
LaryAI's avatar
Level 58

To seed unique pairs between users and producers, you can use a loop to manually create the unique combinations and then use the UserProducer factory to create the relationship. Here's an example of how you can do this:

$users = User::factory(10)->create();
$producers = Producer::factory(20)->create();

// Assuming that UserProducer is the model representing the relationship
// and it has 'user_id' and 'producer_id' as foreign keys

$assignedProducers = [];

foreach ($users as $user) {
    // Shuffle the producers to get a random order
    $shuffledProducers = $producers->shuffle();

    foreach ($shuffledProducers as $producer) {
        $pair = $user->id . '-' . $producer->id;

        // Check if the pair is unique
        if (!in_array($pair, $assignedProducers)) {
            UserProducer::create([
                'user_id' => $user->id,
                'producer_id' => $producer->id,
            ]);

            // Add the pair to the array to keep track of assigned pairs
            $assignedProducers[] = $pair;

            // Break the loop if you only want to assign one unique producer per user
            break;
        }
    }
}

This code snippet does the following:

  1. It creates 10 users and 20 producers.
  2. It iterates over each user.
  3. For each user, it shuffles the list of producers to ensure a random order.
  4. It then iterates over the shuffled producers and creates a unique pair identifier using the user and producer IDs.
  5. It checks if the pair has already been assigned by looking in the $assignedProducers array.
  6. If the pair is unique, it creates a new UserProducer relationship with the user and producer IDs.
  7. It adds the unique pair identifier to the $assignedProducers array to keep track of which pairs have been used.
  8. It breaks the inner loop after assigning one unique producer to the user (remove the break statement if you want to assign more than one unique producer to each user).

Please note that this solution assumes that each user will be assigned only one unique producer. If you want to assign multiple unique producers to each user, you can adjust the inner loop accordingly. Also, ensure that your UserProducer model has the appropriate mass-assignable attributes defined in the $fillable property.

Please or to participate in this conversation.