You must use php artisan to create a custom provider ...
At the command prompt, navigate to the root of your application and type ...
php artisan make:provider FakerServiceProvider
This should create a new provider in the app/Providers folder. Here's what my register function looks like, for example, with the faker file.
/** * Register the application services. * * @return void */ public function register() { $this->app->singleton('Faker', function($app) { $faker = \Faker\Factory::create(); $newClass = new class($faker) extends \Faker\Provider\Base { public function title($nbWords = 5) { $sentence = $this->generator->sentence($nbWords); return substr($sentence, 0, strlen($sentence) - 1); } }; $faker->addProvider($newClass); return $faker; }); }
I am using an anonymous class here. If you have php <7, you will most likely need to create a new file with a new provider class and pass it. Make sure you also add this new provider to your providers array in app/config.php .
Now that it is registered, you can grab your new faker class using the following ...
$faker = app('Faker'); echo $faker->title;
In addition, if you look at the documents at https://laravel.com/docs/5.2/facades , you can also easily make a Faker facade. All the heavy lifting is done, you just need to create a new facade class, getFacadeAccessor return 'Faker' and add it to the facades array in app/config.php .
Then you can just use it like that ...
echo Faker::title;
user3158900
source share