Laravel faker 根据名字生成性别

Laravel faker generate gender from 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')
    ];

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

希望这符合您的要求。

要在没有额外变量的情况下做到这一点,就这样做

return [
  'gender' => $faker->randomElements(['male', 'female']),
  'name' => $faker->name(function (array $user) {return $user['gender'];})
]

希望对您有所帮助

  1. 方法 randomElements 将 return 一个只有一个元素的数组,所以如果你想得到 'female' 或 'male',不要忘记在第一行末尾添加:[0]。您需要结果数组(只有一个元素)的第一个元素(索引 0)。

     $gender = $faker->randomElements(['male', 'female'])[0];
    
  2. 还有一件事。为了得到你想要的,你需要使用 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')
     ];
    
  3. 最后一件事:如果您使用 'Male' 和 'Female',而不是 'male' 和 'female',这是 不会 行得通!!

实际上所有的答案都不适合我。它只返回女性或男性。 我有几乎相同的问题。我需要三种性别中的随机元素。它总是给我这个错误信息:

 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 ⚧