Laravel Factory – generate fake json array data with random amount of iterations in a single column

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


Any assistance is greatly appreciated

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 [].

>Solution :

Don’t make it complicated. Just generate a random number and then generate that many reviews.

$reviews = [];
$amount = rand(1,10);

for($x = 0; $x < $amount; $x++ ){
   $reviews[] = [
        'user' => fake()->name(),
        'body' => fake()->paragraph(),

return [
    "reviews" => json_encode($reviews),

Leave a Reply