使用faker生成性别的最佳方式是什么,生成了一个名称,以便性别与名称相匹配
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'username' => $faker->userName,
'phone' => $faker->phoneNumber,
'gender' => $faker->randomElement(['male', 'female']),//the gender does not match the name as it is.
'address' => $faker->address,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'password' => bcrypt('secret')
];
答案 0 :(得分:6)
查看文档以及他们的Github问题部分提出的问题,您的解决方案似乎是最好的。某些方法允许您为名称指定性别,因此您可以这样做:
$gender = $faker->randomElement(['male', 'female']);
return [
'name' => $faker->name($gender),
'email' => $faker->safeEmail,
'username' => $faker->userName,
'phone' => $faker->phoneNumber,
'gender' => $gender,
'address' => $faker->address,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'password' => bcrypt('secret')
];
希望这符合您的要求。
答案 1 :(得分:2)
方法 randomElements 将返回一个包含单个元素的数组,所以如果你真的想要得到女性的话。 o'男性',不要忘记在第一行末尾添加: [0] 。您需要结果数组的第一个元素(索引0)(只有一个元素)。
$gender = $faker->randomElements(['male', 'female'])[0];
还有一件事。为了准确获取您想要的内容,您需要使用 firstName 而不是名称。这样,名字将根据性别而定。这样做:
return [
'name' => $faker->firstName($gender),
'email' => $faker->safeEmail,
'username' => $faker->userName,
'phone' => $faker->phoneNumber,
'gender' => $gender,
'address' => $faker->address,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'password' => bcrypt('secret')
];
最后一件事:如果你使用男性'而且'女性'而不是男性'并且'女性',不会工作!!
答案 2 :(得分:1)
没有额外的变量就这样做,就像这样做
return [
'gender' => $faker->randomElements(['male', 'female']),
'name' => $faker->name(function (array $user) {return $user['gender'];})
]
希望有所帮助
答案 3 :(得分:0)
实际上,所有答案对我来说都不是很有效。它只返回女性或男性。 我有几乎相同的问题。我需要三种性别中的随机元素。它总是给我这个错误信息:
Illuminate\Database\QueryException : Array to string conversion (SQL: insert
into `users` (`name`, `gender`, `email`, `admin`, `author`, `password`,
`remember_token`) values (Margaret Robel I, male, azieme@example.com, 1, 0,
dummylogin, gwKdVN7zYv))
at /Users/mangrove/Desktop/php-workspace/laravel-
mangrove/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll
format the error
661| // message to include the bindings with SQL, which will make this
exception a
662| // lot more helpful to the developer instead of just the
database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 ErrorException::("Array to string conversion")
/Users/mangrove/Desktop/php-workspace/laravel-
mangrove/vendor/laravel/framework/src/Illuminate/Database/
MySqlConnection.php:80
2 PDOStatement::bindValue()
/Users/mangrove/Desktop/php-workspace/laravel-
mangrove/vendor/laravel/framework/src/Illuminate/
Database/MySqlConnection.php:80
看过Faker v.1.8.0的文档
这对我有用:
public function run()
{
$faker = Faker::create();
foreach(range(1,10) as $index){
// Returns always random genders according to the name, inclusive mixed !!
$gender = $faker->randomElement($array = array('male','female','mixed'));
DB::table('users')->insert([
'name' => $faker->name($gender),
'gender' => $gender,
'email' => $faker->unique()->safeEmail,
'admin' => $faker->numberBetween($min = 0, $max = 1),
'author'=> $faker->numberBetween($min = 0, $max = 1),
'password' => 'dummylogin',
'remember_token' => str_random(10),
]);
}
}
It turns out mixed genders will always have different names, because you can
be either way ⚧