I implemented a new factory to generate random data. But I want to have this random data in the format of de_DE. So usually I create a faker object first, but this is not the case in Laravel 5.1 with the new ModelFactory class. How do I localize this then?
$factory->define(App\Models\AED::class, function($faker) {
return [
'owner' => $faker->company,
'street' => $faker->streetAddress,
'latitude' => $faker->latitude,
'longitude' => $faker->longitude
];
});
In order to change the default locale used by Faker, the easiest way is to simply override the FakerGenerator
binding with your own concrete implementation:
// AppServiceProvider.php
$this->app->singleton(FakerGenerator::class, function () {
return FakerFactory::create('nl_NL');
});
On top of your AppServiceProvider.php
file add the following lines:
use Faker\Generator as FakerGenerator;
use Faker\Factory as FakerFactory;
For example, the above code will mean all Faker instances are created using the nl_NL
provider, thus creating Dutch faker data.
Remember: this has to happen after the DatabaseServiceProvider has been executed, so make sure to put your own AppServiceProvider
after all of the Laravel ServiceProviders in your config.php
array.
Try
$factory->define(App\Models\AED::class, function($faker) {
$faker->locale = "YOUR_LOCALE";
...
});
Add this either at the top of your ModelFactory.php or in your AppServiceProvider::register() method:
$this->app->singleton(\Faker\Generator::class, function () {
return \Faker\Factory::create('de_DE');
});
You must add Providers eg.
$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
];
});
For now You can't set manualy Faker locale. It should be changed on Laravel Core
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.