我正在使用以下代码创建 20 个帖子,每个帖子都有 3 条评论。
Post::factory()
->times(20)
->has(Comment::factory()->times(3))
->create()
相反,我想创建 20 个帖子,每个帖子都有随机数量的评论(例如,帖子 1 有 2 条评论,帖子 2 有 4 条评论,等等)
这不起作用,每个帖子都有相同(随机)数量的评论。
Post::factory()
->times(20)
->has(Comment::factory()->times(rand(1, 5)))
->create()
我怎样才能做到这一点?