16

I'm working with the Faker extension in Laravel 5 to populate my database.

I have "countries" and "Cities" tables so I called

$faker->country

but how can I get a city that is inside that country?

I don't want "Bogotá" to belong to "EEUU".

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115

2 Answers2

14

You'd make a generator and add only the provider for that country (a list of them can be found here):

$faker = new Faker\Generator();
$faker->addProvider(new Faker\Provider\en_AU\Address($faker));
$faker->state; // will give you only Australian states

If your specific need isn't covered by the available providers, you may need to create a custom provider.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • @Ahmed It's very possible Faker's syntax has changed slightly in five years, but "not working" is hardly very useful feedback I can address. – ceejayoz Jun 25 '20 at 18:11
  • how would you do this dynamically? ie. first randomly select a country (Australia) then get a state from it? – matt Aug 16 '20 at 07:35
-1

In Laravel factory,This should do the trick.

use Faker\Provider\en_AU\Address;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'country' => $this->faker->country(),
            'city' => $this->faker->city(),
            'state' => Address::state(),
        ];
    }