gpt4 book ai didi

laravel - 如何在 Laravel 5 中将 Faker 的随机名字和姓氏连接为全名

转载 作者:行者123 更新时间:2023-12-04 23:42:36 26 4
gpt4 key购买 nike

我正在尝试在 Laravel 5 中使用 Faker。现在我需要在我的用户表中创建一些用户,我选择 Faker。

我知道如何创建随机名字、姓氏或用户名,但我想将每个 FN 和 LN 连接为用户名,我该怎么做?这是我在种子文件中的代码。

public function run()
{
$faker = Faker::create();

foreach(range(1, 10) as $index) {
User::create([
'first_name' => $faker->firstName($gender = null|'male'|'female'),
'last_name' => $faker->lastName,
'username' => $faker->userName(),
'email' => $faker->email,
'password' => bcrypt($faker->password(6))
]);
}
}

image of seeder content

最佳答案

您只需创建伪造者 firstNamelastName在 foreach 循环中,然后将它们连接起来以获得 username .

public function run()
{
$faker = Faker::create();

foreach(range(1, 10) as $index) {
$firstName = $faker->firstName;
$lastName = $faker->lastName;
User::create([
'first_name' => $firstName,
'last_name' => $lastName,
'username' => $firstName.$lastName,
'email' => $faker->email,
'password' => bcrypt($faker->password(6))
]);
}
}

回答你的第二个问题。这取决于您想要的电子邮件地址。
public function run()
{
$faker = Faker::create();

foreach(range(1, 10) as $index) {
$firstName = $faker->firstName;
$lastName = $faker->lastName;
$username = $firstName.$lastName;

// username @ specific url
$email = $username.'@exampple.ca';

// first initial last name @ specific url
$email = $firstName[0].$lastName.'@exampple.ca';

// username @ random email safe url
$email = $username.'@'.$faker->safeEmailDomain;

// first initial last name @ random email safe url
$email = $firstName[0].$lastName.'@'.$faker->safeEmailDomain;

User::create([
'first_name' => $firstName,
'last_name' => $lastName,
'username' => $username,
'email' => $email,
'password' => bcrypt($faker->password(6))
]);
}
}

关于laravel - 如何在 Laravel 5 中将 Faker 的随机名字和姓氏连接为全名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33424195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com