Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Faker Registry Laravel Package

laravel-shift/faker-registry

Central registry for Laravel Faker providers. Easily register, discover, and reuse custom providers across apps and packages, keeping factories and seeders consistent. Ideal for sharing domain-specific fake data generators and avoiding duplicate setup.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer:
    composer require laravel-shift/faker-registry
    
  2. Register the service provider (auto-discovered in Laravel 5.5+, but verify in config/app.php if needed).
  3. Create a Faker provider (e.g., app/Faker/Provider/MyCustomProvider.php):
    namespace App\Faker\Provider;
    
    use Faker\Generator;
    
    class MyCustomProvider {
        public static function provide(Generator $faker) {
            return [
                'userAgent' => $faker->randomElement(['Chrome', 'Firefox', 'Safari']),
                'statusCode' => $faker->randomElement([200, 404, 500]),
            ];
        }
    }
    
  4. Register the provider in AppServiceProvider@boot:
    use Faker\Registry;
    
    public function boot()
    {
        Registry::addProvider(\App\Faker\Provider\MyCustomProvider::class);
    }
    
  5. Use it in factories/tests:
    $userAgent = Registry::faker()->userAgent;
    

Implementation Patterns

  • Factory Centralization: Register reusable providers (e.g., address, currency, timeline) once, avoiding per-factory duplication.
  • Test Consistency: Resolve the same Registry::faker() instance across tests to ensure deterministic outputs (e.g., Registry::faker()->countryCode always uses your defined region logic).
  • Custom Logic Encapsulation: Move domain-specific logic (e.g., ::invoiceNumber(), ::productSku()) into providers for clean factory definitions:
    // Factory
    'sku' => Registry::faker()->productSku,
    
  • Lazy Provider Registration: Register providers conditionally (e.g., per environment or feature flag) to avoid polluting test runners unnecessarily.
  • Fallback Chain: Combine with Faker’s built-in addProvider() to layer custom + core providers.

Gotchas and Tips

  • No Service Container Binding: Registry is a static facade over Faker—do not inject it into other services; resolve via Registry::faker() only where needed.
  • Faker Instance Scope: Registry::faker() returns the same Generator instance after first access. For test isolation (e.g., seed control), manually reseed:
    Registry::faker()->seed(1234);
    
  • Provider Conflicts: If two providers define the same method name, the last registered wins. Use descriptive method names (e.g., ::billingAddress() vs ::shippingAddress()).
  • Testing: Mock Registry::faker() via tap(Registry::faker(), fn($f) => $f->seed($seed)) to ensure reproducible test data.
  • Upgrade Caution: No commits since 2022—verify compatibility with Laravel 10+. Consider forking if heavy customization is needed.
  • Custom Generator Extension: Extend Generator and bind your own instance to Laravel’s container, then override Registry::setFaker() to integrate with DI.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport