British Faker data in Laravel model factories

October 28, 2015

development

If you use Laravel you should be using Model Factories. If you’re then check out this introduction to Model Factories on Laravel News.

The problem I’ve struggled with for a while is by default the instance of Faker is set to use en_US. This is’t often a problem, until you start generating phone numbers and postal addresses.

Luckily there is an easy way to fix this—thanks to dependency injection. Open up app/Providers/AppServiceProvider.php and within the register method add this:

$this->app->singleton(FakerGenerator::class, function () {
  return FakerFactory::create('en_GB');
});

Now the Faker instance injected into the Model Factories will be localised—you can obviously change the locale to whatever you need. You can see the available Faker locales here.