问题
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?
回答1:
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;
来源:https://stackoverflow.com/questions/57986143/how-with-faker-word-get-unique-value