How with $faker->word get unique value?

微笑、不失礼 提交于 2021-01-28 07:44:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!