Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gizmojo's avatar

Faker get matching country and code from locale

I'm trying to create random language with real country/code/etc but everything in faker is random even with locale set.

$locale = config('app.faker_locale');
$faker = fake($locale);
$name = $faker->country;
$code = $faker->countryCode;
$iso = $faker->countryISOAlpha3;

Country::create([
    'name' => $name,
    'locale' => $locale,
    'iso' => $iso,
    'code' => $code,
]);

Is there a way of getting the whole country details?

0 likes
2 replies
LaryAI's avatar
Level 58

You can use the Faker\Provider\Base class to get the country details you need. The country method will return an array with the country name, code, and ISO Alpha 3 code.

$locale = config('app.faker_locale');
$faker = Faker\Factory::create($locale);
$country = $faker->country;

Language::create([
    'name' => $country['name'],
    'locale' => $locale,
    'iso' => $country['iso_alpha3'],
    'code' => $country['country_code'],
]);
gizmojo's avatar

This is returning the country name only

$country = $faker->country;
1 like

Please or to participate in this conversation.