Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 843 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change Faker Locale in Laravel 5.2

#1
Is there a way to specify the Faker locale in the database/factories/ModelFactory.php file ? Here is my non functioning attempt at doing so >,<

$factory->define(App\Flyer::class, function (Faker\Generator $faker) {

// What is the correct way of doing this?
$faker->locale('en_GB');

return [
'zip' => $faker->postcode,
'state' => $faker->state,
];
});

Thanks for reading!
Reply

#2
Late in the party, but after some research I've found this in faker documentation:

> [...] since Faker starts with the last provider, you can easily override existing formatters: just add a provider containing methods named after the formatters you want to override.
>
> That means that you can easily add your own providers to a Faker\Generator instance.

So you can do something like this

$faker->addProvider(new Faker\Provider\pt_BR\Person($faker));
Just before `return []` and then use specific providers, like (in this case) `$faker->cpf;`

**Tested on Laravel 5.3**

More info on [Faker documentation][1]


[1]:

[To see links please register here]

Reply

#3
@IvanAugustoDB, there is a another form of doing that. When Laravel initalize faker, it can be constructed on another locale, just create a Service Provider and put the following snippet inside it.


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


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

#4
I prefer to use it:

$fakerBR = Faker\Factory::create('pt_BR');

$factory->define(App\Flyer::class, function (Faker\Generator $faker) use (fakerBR) {

return [
'name' => $fakerBR->name,
'cpf' => $fakerBR->cpf,
'zip' => $faker->postcode,
'state' => $faker->state,
];
});

Reply

#5
Faker locale can be configured in the `config/app.php` configuration file. Just add the key `faker_locale`.

e.g.: `'faker_locale' => 'fr_FR',`

See also my PR to document that previously undocumented feature:

[To see links please register here]

Reply

#6
$factory->define(App\User::class, function (Faker\Generator $faker) {
$faker->addProvider(new Faker\Provider\ng_NG\Person($faker));
$faker->addProvider(new Faker\Provider\ng_NG\PhoneNumber($faker));
...

in the above code, "ng_NG" is for Nigeria and can be replaced with any other locale.

To my knowledge, you would have to specify Person, PhoneNumber and others depending on what you have in your vendor\fzaninotto\faker\src\Faker\Provider folder. However if the provider you intend using isn't available, then it will resolve back to using "en".

This works like charm for me, and it should work for you too.
Reply

#7
This answer is valid just for Laravel 5.4 and greater:

Since [this][1] pull, you can just use 'faker_locale' as a variable in your app config file. It just works really good.


[1]:

[To see links please register here]

Reply

#8
**THIS ANSWER IS ONLY VALID FOR LARAVEL <=5.1 OR WHEN YOU WANT TO USE MANY DIFFERENT LOCALES** see [this answer for a solution in Laravel >=5.2][1].

To use a locale with Faker, the generator needs creating with the locale.

$faker = Faker\Factory::create('fr_FR'); // create a French faker

From the faker documentation:

> If no localized provider is found, the factory fallbacks to the default locale (en_EN).

Laravel by default, binds the creation of a faker instance in the `EloquentServiceProvider`. The exact code used to bind is:

// FakerFactory is aliased to Faker\Factory
$this->app->singleton(FakerGenerator::class, function () {
return FakerFactory::create();
});

It would appear that Laravel has no way to modify the locale of the faker instance it passes into the factory closures as it does not pass in any arguments to Faker.

As such you would be better served by using your own instance of Faker in the factory method.

$localisedFaker = Faker\Factory::create("fr_FR");

$factory->define(App\Flyer::class, function (Faker\Generator $faker) {

// Now use the localisedFaker instead of the Faker\Generator
// passed in to the closure.
return [
'zip' => $localisedFaker->postcode,
'state' => $localisedFaker->state,
];
});


[1]:

[To see links please register here]

Reply

#9
If you are using multiple languages for the same table and can't use local
you can use: shuffleString

'name'=>$faker->shuffleString('abddefhig')
'name_ar'=>$faker->shuffleString('البتثجحخدزسش')
Reply

#10

this is the link for all [providers][1] that used in faker


[1]:

[To see links please register here]


for arabic lang example

use Faker\Factory as Faker; ### in the head off class

$faker = Faker::create();
$faker_ar = Faker::create('ar_SA');

for ($i = 0; $i < 10; $i++) {
DB::table('categories')->insert([
'name' => $faker->name,
'name_ar' => $faker_ar->name,
'created_at' => now(),
'updated_at' => now(),
]);
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through