with this?
factory(App\User::class, 50)->create()->each(function ($u) {
$u->comments()->factory(App\Comment::class)->create();
});
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to generate fake data with faker PHP library but I want for example create 3 comments for each user. how should I do this ? I do create 1 comment for each user with this code :
factory(App\User::class, 50)->create()->each(function ($u) {
$u->comments()->save(factory(App\Comment::class)->make());
});
you can make a loop in the foreach closure like this:
factory(App\User::class, 50)->create()->each(function ($u) {
for ($x = 0; $x <= 2; $x++) {
$u->comments()->factory(App\Comment::class, 3)->create();
}
});
Please or to participate in this conversation.