我想在我的faker中创建两个独特的用户。我想知道正确的做法。
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
{
'name' => "Person 1",
'email' => "person1@gmail.com",
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
'role' => 'super',
},
{
'name' => "Person 2",
'email' => "person2@gmail.com",
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
'role' => 'admin',
},
];
});
答案 0 :(得分:1)
此代码解决了我的问题
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->unique()->randomElement($array = array ('Person1','Person2')),
'email' => $faker->unique()->randomElement($array = array ('p1@gmail.com','p2@gmail.com')),
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
'role' => $faker->unique()->randomElement($array = array ('super','admin')),
];
});
答案 1 :(得分:0)
此代码解决了我的问题:
do {
$uniqueName = $faker->name;
$uniqueEmail = $faker->unique()->safeEmail;
} while (User::where([
'name' => $uniqueName,
'email' => $uniqueEmail,
])
->count()
);