'localization Faker\Factory does not work in laravel

I have a Post Model with these fields :

post_id
post_title
post_content
post_content_full
author

Now I want to use laravel sedders and model factories to create fake fa_IR localized data and insert to posts table.

For that I wrote this in database/factories/ModelFactory.php:

$factory->define(App\Post::class, function (Faker\Generator $faker) {
        return [
            'post_title'        => $faker->sentence,
            'post_content'      => $faker->paragraph,
            'post_content_full' => $faker->paragraph(3),
            'author'            => $faker->name
        ];
    });

Then I created a PostsTableSeeder class like this :

use Illuminate\Database\Seeder;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run ()
    {
        factory(App\Post::class, 5)->create();
    }
}

And in AppServiceProvider.php added below codes to register function :

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

But After running the seed , laravel uses default locale (en_US) and ignores fa_IR.

I do not know what else to do.

Update:

Even I changed in DEFAULT_LOCALE const vendor/fzaninotto/faker/src/Faker/Factory.php to fa_IR Nothing changed.



Solution 1:[1]

Try this way

$factory->define(App\Post::class, function () {
    $faker = Faker\Factory::create('fa_IR');

    return [
        'post_title'        => $faker->sentence,
        'post_content'      => $faker->paragraph,
        'post_content_full' => $faker->paragraph(3),
        'author'            => $faker->name
    ];
});

Solution 2:[2]

You need to change the faker locale in your app config file.

Solution 3:[3]

First run this

php artisan make:factory PostFactory

Do like this

$faker = \Faker\Factory::create();

Then use like this

    $sub_g->name = $faker->name();
    $sub_g->country = $faker->country();
    $sub_g->state = $faker->state;

Thank me later.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdullah Al Shakib
Solution 2 stardust4891
Solution 3 MUHINDO