In ListingFactory.php I have something like this
return [
'reviews' => json_encode([
'user' => fake()->name(),
'body' => fake()->paragraph(),
]),
]
Additionally, in the DatabaseSeeder.php I have this at the moment
AppModelsListing::factory(10)->create();
The current problem is that it will always generate one instance of review. What I want is a random number of reviews in a range.
For example, right now the table column of Review will always be [{}]
, I want something like [{}, {}, {}]
or []
.
3
Answers
Simply generate your fake data in loop.
I would believe that
randomElements()
can do what you want, it takesx
elements from a sequence. Combine that with a random number and you would achieve that.If you would change the amount of reviews, increase random_int(1, 10) for a sequence of 10.
Don’t make it complicated. Just generate a random number and then generate that many reviews.