使用伪造者生成性别的最佳方法是什么,它已经生成了一个名称以使性别与名称匹配

 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')
    ];

最佳答案

查看文档以及在其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')
];

希望这符合您的要求。

关于php - Laravel Fakerr从名字中产生性别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43138197/

10-10 00:07