我是Laravel的新手,我想为表用户生成虚假数据。 对,知道这工作正常:
UserFactory.php
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});
UsersTableSeeder.php
public function run() {
factory(App\User::class, 5)->create();
}
DatabaseSeeder.php
public function run() {
$this->call(UsersTableSeeder::class);
}
前面的代码在User表中生成了5行,没有问题。
现在我想再生一个额外的用户( 除了已生成的5行外,还有固定字段,如:
return [
'name' => 'user test',
'email' => 'user@mycompany.com',
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
我必须把上面的代码放在哪里?我是否必须生成新的UserFactory?
答案 0 :(得分:2)
您可以在现有代码后面的UsersTableSeeder.php
run()
方法中添加以下代码:
factory(App\User::class)->create([
'name' => 'user test',
'email' => 'user@mycompany.com',
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
]);
您还可以使用'password' => bcrypt(USERPASSWORDHERE),
创建加密的USERPASSWORDHERE字符串