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 it?
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 it?
I found this worked better, as it did not require my
$faker
instance to be instantiated withresolve()
:You should use
php artisan
to generate the custom provider...On the command line, navigate to the root of your app and type...
That should generate a new provider in the
app/Providers
folder. Here is what my register function looks like going off the example in the faker docs.I'm using an anonymous class here. If you have php < 7, you would likely need to create a new file with your new provider class and pass that in. Make sure you also add this new provider to your
providers
array inapp/config.php
.Now that it's registered, you can grab your new faker class using the following...
Additionally, if you go through the docs at https://laravel.com/docs/5.2/facades you should also be able to make a
Faker
facade quite easily. All the heavy lifting is done, you'd just have to create the new facade class, havegetFacadeAccessor
return'Faker'
, and add it to yourfacades
array inapp/config.php
.Then you can simply use it like so...
Create your custom provider class and save it under app/Faker/CustomProvider.php. Code:
Then you need just add your custom provider to faker by addProvider method. Example of laravel's factory with adding custom provider: