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
Create your custom provider class and save it under app/Faker/CustomProvider.php. Code:
namespace App\Faker;
use Faker\Provider\Base;
class CustomProvider extends Base
{
public function customName()
{
return $this->generator->sentence(rand(2, 6));
}
}
Then you need just add your custom provider to faker by addProvider method. Example of laravel's factory with adding custom provider:
define(App\Models\Model::class, function(Faker $faker) {
$faker->addProvider(new App\Faker\CustomProvider($faker));
return [
'name' => $faker->customName,
];
});