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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33424195/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:35:53  来源:igfitidea点击:

How to concatenate Faker's random first name and its last name to a full name in Laravel 5

laravelrandomfaker

提问by bill dou

I am trying to use Faker in Laravel 5. Now I need to create some users in my User table, I choose Faker.

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

I know how to create random firstname, lastname or userName, but I want to concatenate each FN and LN to be username, how can I do that? Here is my codes in seeder file.

我知道如何创建随机的名字、姓氏或用户名,但我想将每个 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

播种机内容的图像

回答by whoacowboy

You would just create the faker firstNameand lastNamein the foreach loop and then concatenate them for the username.

你只需创建摊贩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))
        ]);
    }
}

To answer your second question. It depends on your desired email address.

回答你的第二个问题。这取决于您想要的电子邮件地址。

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