2

In laravel 5.8 app I make tests and adding new user for any tests I encountered that line

$loggedUser= factory(User::class)->create();

raise error :

Doctrine\DBAL\Driver\PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Username pariatur' for key 'users_username_unique'

with factory defined :

$factory->define(App\User::class, function (Faker $faker) {
    $username= 'Username ' . $faker->word;
    return [
        'username' => $username,

I do not clear database, but how to make in series of tests get unqiue word for any test?

mstdmstd
  • 2,195
  • 17
  • 63
  • 140

1 Answers1

4

Faker provides three special providers like unique(), optional(), and valid(), to be called before any provider.

//use unique() before calling the method
$faker->unique()->name;
Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81