2017-08-18 67 views
1

我想在我的faker中創建兩個獨特的用戶。我想知道正確的做法。Laravel:我如何在我的laravel faker中生成兩個獨特的種子

$factory->define(App\User::class, function (Faker\Generator $faker) { 
    static $password; 

    return [ 
     { 
     'name' => "Person 1", 
     'email' => "[email protected]", 
     'password' => $password ?: $password = bcrypt('secret'), 
     'remember_token' => str_random(10), 
     'role' => 'super', 
     }, 
     { 
     'name' => "Person 2", 
     'email' => "[email protected]", 
     'password' => $password ?: $password = bcrypt('secret'), 
     'remember_token' => str_random(10), 
     'role' => 'admin', 
     }, 
    ]; 
}); 

回答

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 ('[email protected]','[email protected]')), 
    'password' => $password ?: $password = bcrypt('secret'), 
    'remember_token' => str_random(10), 
    'role' => $faker->unique()->randomElement($array = array ('super','admin')), 
]; 
}); 
+0

什麼本作是 - '$陣列='? – zhekaus

+0

我在數組變量 –

+0

裏面存儲了唯一值爲什麼?之後你就不用它了。 – zhekaus