42
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelのFakerを日本語化する

Posted at

Laravelには標準でFakerが含まれていて、ダミーデータを作るのに便利です。
https://laravel.com/docs/5.5/database-testing#writing-factories

Fakerが生成してくれるダミーデータは設定を変える事によって日本語にすることができます。

Fakerの初期化処理は

laravel/framework/src/Illuminate/Database/DatabaseServiceProvider.php
 /**
     * Register the Eloquent factory instance in the container.
     *
     * @return void
     */
    protected function registerEloquentFactory()
    {
        $this->app->singleton(FakerGenerator::class, function ($app) {
            return FakerFactory::create($app['config']->get('app.faker_locale', 'en_US'));
        });

        $this->app->singleton(EloquentFactory::class, function ($app) {
            return EloquentFactory::construct(
                $app->make(FakerGenerator::class), $this->app->databasePath('factories')
            );
        });
    }

という風になっており、初期化時のlocaleを設定で変更可能です。

よって設定ファイルに設定を追記します。

config/app.php
'faker_locale' => 'ja_JP',

完了です。

42
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?