Home > Software design >  Factory creating multiple models with different number of relationships
Factory creating multiple models with different number of relationships

Time:10-07

I'm using the following code to create 20 posts, each of which has 3 comments.

Post::factory()
    ->times(20)
    ->has(Comment::factory()->times(3))
    ->create()

Instead I'd like to create 20 posts, each of which has a random number of comments (e.g. post 1 has 2 comments, post 2 has 4 comments, etc.)

This did not work, each post had the same (random) number of comments.

Post::factory()
    ->times(20)
    ->has(Comment::factory()->times(rand(1, 5)))
    ->create()

How can I achieve this?

CodePudding user response:

It's not possible to have a dynamic number of related models per model if you are using ->times as far as I know. You can instead try:

collect(range(0,19))
   ->each(function () {
       Post::factory()
          ->has(Comment::factory()->times(rand(1,5)))
          ->create();
});

This should create 20 posts one by one with a random number of comments on each. It may be a bit slower but probably not by much

CodePudding user response:

I would use a factory method to do this. Add a method to your Post factory like this:

public function addComments($count = null): self
{
    $count = $count ?? rand(1, 5);

    return $this->afterCreating(
        fn (Post $post) => Comment::factory()->count($count)->for($post)->create()
    );
}

Then in your test, you can simply call it like this:

Post::factory()->count(20)->addComments()->create();

CodePudding user response:

Updated: That should work: Inspired by apokryfos. If that not work, that will:

for($i=0; $i<20; $i  )
{
    $times = rand(1,5);
    Post::factory()
     ->has(Comment::factory()->times($times))
     ->create();
}
  • Related