Edgard Bertelli
Edgard Bertelli

Reputation: 47

How can I create a random name using Faker formatters in Laravel?

So I'm generating fake data using Faker framework.

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Paciente;
use Faker\Generator as Faker;

$factory->define(Paciente::class, function (Faker $faker) {
    return [
        'nm_paciente' => $faker->name,
        'data_nascimento' => $faker->date(),
        'email' => $faker->email,
        'sexo' => random_int(1, 2),
        'cidade_id' => random_int(1, 7),
        'estado_id' => random_int(1, 27),
        'status_id' => random_int(1, 2)
    ];
});

But now I need to generate brazilian portuguese (pt_BR) names by using the Faker formatters and I don't know how. I tried to instance it but didn't work. Can somebody help me with that?

Upvotes: 0

Views: 6987

Answers (2)

tarek suvo
tarek suvo

Reputation: 81

Faker also supports multiple locales. New in v3.0.0.

from faker import Faker
fake = Faker(['it_IT', 'en_US', 'ja_JP'])
for _ in range(10):
    print(fake.name())

# 鈴木 陽一
# Leslie Moreno
# Emma Williams
# 渡辺 裕美子
# Marcantonio Galuppi
# Martha Davis
# Kristen Turner
# 中津川 春香
# Ashley Castillo
# 山田 桃子

Check Documentation

Upvotes: 1

Watercayman
Watercayman

Reputation: 8178

You can use addProvider to assign specific language for an element.

$factory->define(Paciente::class, function (Faker $faker) {
   $faker->addProvider(new \Faker\Provider\pt_BR\Person($faker));

   return [
    'nm_paciente' => $faker->name, // Portuguese  name

If you would like it to be in Portuguese for everything from Faker, you can set it up in your app.php file like this:

'faker_locale' => 'pt_BR',

Hope this helps

Upvotes: 0

Related Questions