0

I am using laravel 9. I am trying to generate some fake users to boost my db using factoreis and seeders.
When i try to seed my db i get this error

    Object of class Faker\UniqueGenerator could not be converted to string

  at D:\PROJECTS\LARAVEL\todo-list\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665
    661▕                 $value,
    662▕                 match (true) {
    663▕                     is_int($value) => PDO::PARAM_INT,
    664▕                     is_resource($value) => PDO::PARAM_LOB,
  ➜ 665▕                     default => PDO::PARAM_STR
    666▕                 },
    667▕             );
    668▕         }
    669▕     }

  1   D:\PROJECTS\LARAVEL\todo-list\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665
      PDOStatement::bindValue(Object(Faker\UniqueGenerator))

  2   D:\PROJECTS\LARAVEL\todo-list\vendor\laravel\framework\src\Illuminate\Database\Connection.php:540
      Illuminate\Database\Connection::bindValues(Object(PDOStatement))

this my UserFactory

     class UserFactory extends Factory
        {
            /**
             * Define the model's default state.
             *
             * @return array<string, mixed>
             */
            public function definition()
            {
                return [
                    'name' => fake()->name(),
                    'username' => fake()->unique(),
                    'company' => fake()->sentence(),
                    'position' => fake()->sentence(),
                    'bio' => fake()->realText($maxNbChars = 100),
                    'picture' => fake()->imageUrl(90, 90),
                    'email' => fake()->unique()->safeEmail(),
                    'email_verified_at' => now(),
                    'password' => Hash::make('password'), // password
                    'remember_token' => Str::random(10),
                ];
            }
        
            /**
             * Indicate that the model's email address should be unverified.
             *
             * @return static
             */
            public function unverified()
            {
                return $this->state(fn (array $attributes) => [
                    'email_verified_at' => null,
                ]);
            }
        }

After looking arround for a while I found that fake()->unique() does not return a string. So I tried to convert it to string but It also gives me a error saying Unknown format "toString" in Faker\Generator.php:731

1 Answers1

0

[Problem solved]

I just had to edit my UserFactory username to this

'username' => fake()->unique()->text(16),