Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Factory creating multiple models with different number of relationships

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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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

<?php
namespace Database\Factories\App;

use App\Comment;
use App\Post;
use Illuminate\Database\Eloquent\Factories\Factory;

class PostFactory extends Factory
{
    public function definition(): array
    {
        return [
            // ...
        ];
    }

    public function addComments(int $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();
over 4 years ago · Santiago Trujillo Report

0

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();
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!