How to implement your own Faker provider in Laravel

后端 未结 3 1565
独厮守ぢ
独厮守ぢ 2021-02-07 08:50

I want to create a custom provider for Faker in Laravel (e.g. one for a random building name).

Where do I store the custom provider in my application and how do I use i

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 09:00

    I found this worked better, as it did not require my $faker instance to be instantiated with resolve():

    public function register ()
        {
            $this->app->bind( Generator::class, function ( $app ) {
    
                $faker = \Faker\Factory::create();
                $faker->addProvider( new CustomFakerProvider( $faker ) );
    
                return $faker;
            } );
    
        }
    

提交回复
热议问题