php Laravel faker从名称生成性别

flseospp  于 2023-01-24  发布在  PHP
关注(0)|答案(4)|浏览(222)

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

k2arahey1#

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

希望这符合您的要求。

6ie5vjzr

6ie5vjzr2#

要在不使用其他变量的情况下执行此操作,请按以下方式执行

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

希望能有所帮助

5ssjco0h

5ssjco0h3#

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

$gender = $faker->randomElements(['male', 'female'])[0];

1.还有一件事。为了得到你想要的东西,你需要用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')
 ];

1.最后一件事:如果你用“男性”和“女性”,而不是“男性”和“女性”,这是去工作!

vom3gejh

vom3gejh4#

实际上,并非所有答案都对我有效。它只返回女性或男性。我遇到了几乎相同的问题。我需要从三种性别中随机选择一个元素👍😀。它总是给我这样的错误信息:

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 😄⚧

相关问题