Laravel faker 从名字生成性别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43138197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel faker generate gender from name
提问by Morris Mukiri
What is the optimum way of generating gender using faker, having generated a name so that the gender matches the name
使用 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')
];
回答by James
Looking at the documentation and an issue raised on the their Github issues section, your solution seems to be the best. Some methods allow you to specify the gender for a name so you could do like this:
查看文档和在他们的 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')
];
Hopefully this fits your requirement.
希望这符合您的要求。
回答by Reinier Garcia
The method randomElementsis gonna return an array with one single element, so if you really want to get 'female' o 'male', don't forget to add at the end of the first line this: [0]. You need the first element (index 0) of the resulting array (that only has one element).
$gender = $faker->randomElements(['male', 'female'])[0];
One more thing. In order to obtain exactly what you want, you need to use firstNameinstead of name. This way the first name will be according to the gender. Do it this way:
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') ];
One last thing: If you use 'Male' and 'Female', instead of 'male' and 'female', this is NOTgonna work!!
方法randomElements将返回一个包含一个元素的数组,所以如果你真的想得到 'female' o 'male',不要忘记在第一行的末尾添加:[0]。您需要结果数组的第一个元素(索引 0)(只有一个元素)。
$gender = $faker->randomElements(['male', 'female'])[0];
还有一件事。为了准确获得您想要的内容,您需要使用firstName而不是 name。这样,名字将根据性别。这样做:
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') ];
最后一两件事:如果你用“男”和“女”,而不是“男”和“女”,这是不是要去工作!
回答by Bang Fady
to do it without additional variable, do it like this
在没有额外变量的情况下做到这一点,这样做
return [
'gender' => $faker->randomElements(['male', 'female']),
'name' => $faker->name(function (array $user) {return $user['gender'];})
]
hope it helps
希望能帮助到你
回答by mangrove108
Actually all answers did not really work for me. It returned female or male only. I had almost the same issue. I needed a random element out of three genders . It always gave me this error message:
实际上,所有答案都对我不起作用。它只返回女性或男性。我有几乎同样的问题。我需要三个性别中的一个随机元素。它总是给我这个错误信息:
Illuminate\Database\QueryException : Array to string conversion (SQL: insert
into `users` (`name`, `gender`, `email`, `admin`, `author`, `password`,
`remember_token`) values (Margaret Robel I, male, [email protected], 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
After a look at the documentation for Faker v.1.8.0
查看Faker v.1.8.0的文档后
This worked for me:
这对我有用:
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 ?