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 Laravel Package

splash/faker

Laravel/PHP package providing Faker helpers and integrations to generate realistic fake data for testing and seeding. Includes utilities to quickly create names, emails, addresses, dates, and more, streamlining factories and test setups.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require splash/faker
    

    Ensure your project uses Symfony Flex (or manually register the bundle in config/bundles.php):

    return [
        // ...
        Splash\FakerBundle\SplashFakerBundle::class => ['all' => true],
    ];
    
  2. Configuration Publish the default config (if needed):

    php bin/console splash:faker:install
    

    Verify config/packages/splash_faker.yaml exists.

  3. First Use Case Generate fake data in a controller or service:

    use Splash\FakerBundle\Generator\FakerGenerator;
    
    class DemoController extends AbstractController
    {
        public function generateData(FakerGenerator $generator): Response
        {
            $fakeUser = $generator->generate('user', ['name' => 'John Doe']);
            return $this->json($fakeUser);
        }
    }
    

Implementation Patterns

Core Workflows

  1. Data Generation Use predefined generators (e.g., user, address, product) via dependency injection:

    $generator->generate('user', ['age' => 30]); // Returns fake user with age=30
    
  2. Custom Data Types Extend the bundle by defining new generators in config/packages/splash_faker.yaml:

    splash_faker:
        generators:
            custom_entity:
                class: App\Service\CustomEntityGenerator
                arguments: ['@service_id']
    
  3. Integration with Forms Seed forms with fake data during development:

    $form = $this->createForm(UserType::class, $generator->generate('user'));
    
  4. Bulk Generation Use the CLI tool for batch generation:

    php bin/console splash:faker:generate user 100 > users.json
    

Best Practices

  • Dependency Injection: Prefer injecting FakerGenerator over instantiating directly.
  • Environment Awareness: Disable fake generation in production via config:
    splash_faker:
        enabled: '%kernel.environment% != "prod"'
    
  • Testing: Use the generator in PHPUnit tests for isolated test data:
    public function testUserCreation()
    {
        $fakeUser = $this->container->get(FakerGenerator::class)->generate('user');
        $this->assertArrayHasKey('email', $fakeUser);
    }
    

Gotchas and Tips

Common Pitfalls

  1. Generator Registration

    • Issue: Custom generators fail to load.
    • Fix: Ensure the generator class implements Splash\FakerBundle\Generator\GeneratorInterface and is registered in the config.
  2. Performance

    • Issue: Slow bulk generation.
    • Tip: Use splash:faker:generate with --batch-size for memory efficiency:
      php bin/console splash:faker:generate user 1000 --batch-size=100
      
  3. Configuration Overrides

    • Issue: Config changes not reflected.
    • Fix: Clear the cache after modifying splash_faker.yaml:
      php bin/console cache:clear
      

Debugging Tips

  • Log Generation: Enable debug mode to log generated data:
    splash_faker:
        debug: true
    
  • Validate Output: Use the validate command to check generator output:
    php bin/console splash:faker:validate user
    

Extension Points

  1. Custom Data Providers Override default providers by extending Splash\FakerBundle\Provider\AbstractProvider and referencing them in config:

    splash_faker:
        providers:
            custom_provider: App\Provider\CustomProvider
    
  2. Event Listeners Hook into generation lifecycle via events (e.g., splash_faker.generate):

    // src/EventListener/FakerListener.php
    public function onGenerate(FakerEvent $event)
    {
        $event->setData(array_merge($event->getData(), ['custom_field' => 'value']));
    }
    
  3. Database Seeding Combine with Doctrine fixtures for database population:

    use Splash\FakerBundle\Generator\FakerGenerator;
    use Doctrine\Bundle\FixturesBundle\Fixture;
    
    class LoadUserDataFixture extends Fixture
    {
        public function __construct(private FakerGenerator $generator) {}
    
        public function load(ObjectManager $manager)
        {
            $fakeUser = $this->generator->generate('user');
            $user = new User();
            $user->setEmail($fakeUser['email']);
            $manager->persist($user);
        }
    }
    
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.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver