Search code examples
laravellaravel-factory

How to fix error in factory with message textfaker paragraphs or text?


On laravel/livewire site i make factory with message text using faker paragraphs method :

$faker = \Faker\Factory::create();
return [
    'user_id' => $this->faker->randomElement(User::all()->select('id'))['id'],
    'message' => 'message text ' . $this->faker->paragraphs(rand(1, 3), true),
    'created_at' => $faker->dateTimeBetween('-1 month', '-1 minute'),
];

and I got data in db I need. But I need to get randomly paragraphs or some text

$faker = \Faker\Factory::create();
return [
    'user_id' => $this->faker->randomElement(User::all()->select('id'))['id'],
    'message' =>  'message text ' . (rand(1, 4) === 1 ? $this->faker->paragraphs(rand(1, 2)) : $this->faker->text(40)),
    'created_at' => $faker->dateTimeBetween('-1 month', '-1 minute'),
];

I got error :

   Illuminate\Database\QueryException

  Array to string conversion (Connection: mysql, SQL: insert into `messages` (`user_id`, `message`, `created_at`, `updated_at`) values (2, ?, 2024-05-01 06:03:39, 2024-05-07 09:18:10))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

      +17 vendor frames

  18  database/seeders/messages.php:16
      Illuminate\Database\Eloquent\Factories\Factory::create()
      +22 vendor frames

  41  artisan:35
      Illuminate\Foundation\Console\Ker

Which valid way to fix it ?

"laravel/framework": "^10.48.4",
"fakerphp/faker": "^1.23.1",

Thanks in advance!


Solution

  • According to the Faker documentation there is a second param to the paragraphs method, by default echo $faker->paragraphs(2); gives an array of paragraphs and if you want is as text you have to pass true as second param :

    @method array|string paragraphs($nb = 3, $asText = false)
    

    Here is the usage :

    echo $faker->paragraphs(2);
    
    // [
    //     'Quasi nihil nisi enim omnis natus eum. Autem sed ea a maxime. Qui eaque doloribus sit et ab repellat. Aspernatur est rem ut.',
    //     'Corrupti quibusdam qui et excepturi. Fugiat minima soluta quae sunt. Aperiam adipisci quas minus eius.'
    // ]
    
    echo $faker->paragraphs(2, true);
    
    // Quia odit et quia ab. Eos officia dolor aut quia et sed. Quis sint amet aut. Eius enim sint praesentium error quo sed eligendi. Quo id sint et amet dolorem rem maiores.
    //
    // Fuga atque velit consectetur id fugit eum. Cupiditate aut itaque dolores praesentium. Eius sunt ut ut ipsam.
    

    As a note side you can use the fake helper method without the need to create manually an instance :

    return [
        'user_id' => fake()->randomElement(User::all()->select('id'))['id'],
        'message' =>  'message text ' . (rand(1, 4) === 1 ? fake()->paragraphs(rand(1, 2), true) : fake()->text(40)),
        'created_at' => $faker->dateTimeBetween('-1 month', '-1 minute'),
    ];