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

Auto Seeder Laravel Package

oluokunkabiru/auto-seeder

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation: Run composer require oluokunkabiru/auto-seeder in your Laravel project.
  2. First Command: Seed a single row for a model with:
    php artisan seed:auto User
    
    • Replace User with your model name (e.g., Product, Post).
  3. Verify Data: Check your database table for auto-generated, schema-compliant fake data.

Where to Look First

  • Artisan Command: Explore php artisan seed:auto --help for all available flags (e.g., --domain, --skip).
  • Dashboard: Visit /auto-seeder to interactively seed models via a UI.
  • Configuration: Check .env for auto-seeder settings (e.g., AUTO_SEEDER_DEFAULT_COUNT=10).

First Use Case

Quickly populate a development database for testing:

php artisan seed:auto User 50 --domain=example.com --locale=en_US
  • Seeds 50 User records with realistic fake data, using example.com for emails and English locale.

Implementation Patterns

Core Workflows

  1. Schema-Aware Seeding:

    • Automatically introspects table columns (types, ENUMs, foreign keys) to generate valid fake data.
    • Example: Seeding a Post model with a category_id foreign key will auto-populate categories table first if referenced.
  2. Bulk Seeding:

    • Seed multiple rows in one command:
      php artisan seed:auto Order 100
      
    • Useful for load testing or generating large datasets.
  3. Conditional Seeding:

    • Skip sensitive columns (e.g., api_token):
      php artisan seed:auto User 20 --skip=api_token,remember_token
      
    • Override defaults (e.g., email domain, phone country code):
      php artisan seed:auto User 30 --domain=company.com --country-code=+1
      
  4. Locale-Specific Data:

    • Generate locale-aware fake data (e.g., French addresses):
      php artisan seed:auto User 15 --locale=fr_FR
      
  5. Integration with Migrations:

    • Use after running php artisan migrate:fresh to populate a clean database:
      php artisan migrate:fresh --seed
      composer require oluokunkabiru/auto-seeder
      php artisan seed:auto User 5 Product 10
      

Advanced Patterns

  1. Dynamic Seeding in Tests:

    • Seed data on-the-fly in PHPUnit tests:
      use Oluokun\AutoSeeder\Facades\AutoSeeder;
      
      public function test_something()
      {
          AutoSeeder::seed('User', 3); // Seeds 3 users
          $this->assertDatabaseHas('users', ['email' => 'user@example.com']);
      }
      
  2. Custom Faker Providers:

    • Extend Faker to add domain-specific fake data:
      // app/Providers/FakerServiceProvider.php
      use Faker\Generator as Faker;
      
      public function register()
      {
          Faker::addProvider(new class($this->app['faker']) {
              public function customField()
              {
                  return 'custom_value';
              }
          });
      }
      
    • Auto-seeder will use these providers automatically.
  3. Dashboard Customization:

    • Publish and modify views for the /auto-seeder UI:
      php artisan vendor:publish --tag=auto-seeder-views
      
    • Add custom logic to the seed button click handler (e.g., pre-seeding hooks).
  4. Seeding Foreign Key Dependencies:

    • Auto-seeder handles foreign keys by seeding parent tables first. For complex relationships, manually seed parents:
      php artisan seed:auto Category 5  # Seed categories first
      php artisan seed:auto Product 20  # Then seed products
      
  5. Environment-Specific Config:

    • Set defaults in .env:
      AUTO_SEEDER_DEFAULT_COUNT=10
      AUTO_SEEDER_DEFAULT_DOMAIN=example.com
      AUTO_SEEDER_DEFAULT_LOCALE=en_US
      
    • Override via CLI flags as needed.

Gotchas and Tips

Pitfalls

  1. Foreign Key Constraints:

    • If auto-seeder fails to seed a parent table (e.g., categories for products), it will skip the foreign key column. Solution: Seed parent tables manually or ensure all referenced tables exist.
  2. Unique/Indexed Columns:

    • Columns with unique constraints may generate duplicate values. Solution: Use --skip for problematic columns or increase row count to reduce collisions.
  3. Custom Accessors/Mutators:

    • Auto-seeder bypasses Eloquent mutators/accessors. Solution: Seed raw data via DB::table() or handle post-processing:
      $users = User::all();
      foreach ($users as $user) {
          $user->update(['email' => strtolower($user->email)]);
      }
      
  4. Large Datasets:

    • Seeding thousands of rows may hit memory limits. Solution: Use chunks:
      php artisan seed:auto User 1000 --chunk=100
      
  5. Dashboard Caching:

    • Model cards in the UI may not update immediately after adding a new model. Solution: Clear config cache:
      php artisan config:clear
      

Debugging Tips

  1. Verbose Output:

    • Enable debug mode to see generated data:
      php artisan seed:auto User --debug
      
  2. Dry Run:

    • Preview data without inserting:
      php artisan seed:auto User --dry-run
      
  3. Log Generated Data:

    • Redirect output to a file:
      php artisan seed:auto User 10 > seeded_data.log
      
  4. Check Faker Locale:

    • Ensure the locale matches your needs (e.g., fr_FR for French addresses). Test with:
      php artisan seed:auto User 1 --locale=fr_FR --debug
      

Configuration Quirks

  1. Environment Variables:

    • Dashboard settings (locale, domain) are saved to .env but may not reflect immediately. Solution: Restart the server or clear cache.
  2. Model Discovery:

    • The dashboard auto-discovers models in app/Models. Solution: Place models in this directory or configure custom paths via service provider.
  3. Faker Provider Conflicts:

    • If using custom Faker providers, ensure they are registered before auto-seeder runs. Solution: Register in AppServiceProvider@boot.

Extension Points

  1. Custom Data Generators:

    • Extend the seeder by creating a custom generator:
      // app/Services/CustomSeeder.php
      use Oluokun\AutoSeeder\Contracts\Seeder;
      
      class CustomSeeder implements Seeder
      {
          public function seed($model, $count)
          {
              // Custom logic
          }
      }
      
    • Bind it in AppServiceProvider:
      $this->app->bind(Seeder::class, CustomSeeder::class);
      
  2. Pre/Post-Seed Hooks:

    • Add logic before/after seeding via events:
      // app/Providers/EventServiceProvider.php
      protected $listen = [
          'oluokun.auto-seeder.seeding' => [
              'App\Listeners\PreSeedListener',
          ],
          'oluokun.auto-seeder.seeded' => [
              'App\Listeners\PostSeedListener',
          ],
      ];
      
  3. Override Default Faker:

    • Replace the default Faker instance:
      // app/Providers/AppServiceProvider.php
      public function register()
      {
          $this->app->singleton(Faker::class, function () {
              return Faker\Factory::create('fr_FR');
          });
      }
      
  4. Batch Processing:

    • For very large tables, implement batch seeding:
      // app/Console/Commands/BatchSeed.php
      use Oluokun\AutoSeeder\Facades\AutoSeeder;
      
      public function handle()
      {
          AutoSeeder::seed('User', 1000, ['chunk' => 200]);
      }
      
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.
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
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours