Laravel 5.1 Localization Factory Seeder

I created a new factory to generate random data. But I want this random data to be in de_DE format. Therefore, usually I create a faker object first, but this does not apply to Laravel 5.1 with the new ModelFactory class. How do I localize this?

$factory->define(App\Models\AED::class, function($faker) { return [ 'owner' => $faker->company, 'street' => $faker->streetAddress, 'latitude' => $faker->latitude, 'longitude' => $faker->longitude ]; }); 
+6
source share
4 answers

To change the default locale used by Faker, the easiest way is to simply override the FakerGenerator binding with your specific concrete implementation:

 // AppServiceProvider.php $this->app->singleton(FakerGenerator::class, function () { return FakerFactory::create('nl_NL'); }); 

At the top of your AppServiceProvider.php file AppServiceProvider.php add the following lines:

 use Faker\Generator as FakerGenerator; use Faker\Factory as FakerFactory; 

For example, the code above would mean that all instances of Faker are created using the nl_NL provider, thus creating Dutch faker data.

Remember: this should happen after the DatabaseServiceProvider is executed, so be sure to place your own AppServiceProvider after all the Laravel ServiceProviders in your config.php array.

+21
source

Try

 $factory->define(App\Models\AED::class, function($faker) { $faker->locale = "YOUR_LOCALE"; ... }); 
+4
source

Add this either at the top of your ModelFactory.php, or to your AppServiceProvider :: register () method:

 $this->app->singleton(\Faker\Generator::class, function () { return \Faker\Factory::create('de_DE'); }); 
+1
source

You must add providers, for example.

 $factory->define(Mylead\Models\UserDetails::class, function($faker) { $faker->addProvider(new Faker\Provider\pl_PL\Person($faker)); $faker->addProvider(new Faker\Provider\pl_PL\Payment($faker)); return [ 'name' => $faker->name, 'surname' => $faker->lastname, 'street' => $faker->streetName, 'city' => $faker->city, 'post_code' => $faker->pesel, 'pesel' => $faker->pesel, 'paypal' => $faker->email, 'bank_name' => $faker->bank, 'bank_account' => $faker->bankAccountNumber, 'created_at' => $faker->dateTime ]; }); 

While you can not set the manual mode Faker. It should be changed to Laravel Core

0
source

Source: https://habr.com/ru/post/988861/


All Articles