# iSeed - Laravel Inverse Seed Generator iSeed is a Laravel package that generates database seed files from existing database tables. Instead of manually writing seeders, this tool reads data from your database tables and creates properly formatted Laravel seeder classes. This inverse approach to seeding is particularly useful for migrating data between environments, creating test fixtures from production data, or documenting existing database state. The package integrates seamlessly with Laravel's artisan command-line interface and provides extensive configuration options for filtering, chunking, and customizing the generated seed files. It supports all Laravel versions from 5.5 through 12.0, automatically generates seeder class names following Laravel conventions, and updates the DatabaseSeeder.php file to include calls to newly generated seeders. ## Installation Install via Composer and register service provider ```bash # Install latest version (Laravel 5.5+) composer require orangehill/iseed # For Laravel 5.4 and below, add to config/app.php providers array: # Orangehill\Iseed\IseedServiceProvider::class ``` ## Generate Seed from Single Table Generate a seeder file from a database table ```bash # Generate UsersTableSeeder.php from 'users' table php artisan iseed users # Generated file: database/seeders/UsersTableSeeder.php # Content example: ``` ```php insert(array ( 0 => array ( 'id' => '1', 'email' => 'admin@example.com', 'password' => '$2y$10$encrypted_password', 'name' => 'Admin User', 'created_at' => '2024-01-15 10:30:00', 'updated_at' => '2024-01-15 10:30:00', ), 1 => array ( 'id' => '2', 'email' => 'user@example.com', 'password' => '$2y$10$another_encrypted_password', 'name' => 'Regular User', 'created_at' => '2024-01-16 14:22:00', 'updated_at' => '2024-01-16 14:22:00', ), )); } } ``` ## Generate Seeds from Multiple Tables Generate seeder files for multiple tables in one command ```bash # Generate seeders for users, posts, and comments tables php artisan iseed users,posts,comments # Outputs: # - UsersTableSeeder.php # - PostsTableSeeder.php # - CommentsTableSeeder.php ``` ## Generate Seeds for All Database Tables Generate seeders for every table in the database ```bash # Automatically discovers all tables and generates seeders php artisan iseed # DatabaseSeeder.php is automatically updated: ``` ```php call(UsersTableSeeder::class); $this->call(PostsTableSeeder::class); $this->call(CommentsTableSeeder::class); #iseed_end } } ``` ## Limit Number of Rows Export only a specific number of rows from a table ```bash # Generate seeder with only 10 rows from users table php artisan iseed users --max=10 # Combine with ordering to get specific subset php artisan iseed users --max=10 --orderby=created_at --direction=desc # Gets the 10 most recently created users ``` ## Exclude Columns from Export Omit specific columns from generated seeders ```bash # Exclude single column php artisan iseed users --exclude=id # Exclude multiple columns php artisan iseed users --exclude=id,created_at,updated_at # Generated seeder will not include excluded columns ``` ```php insert(array ( 0 => array ( 'email' => 'admin@example.com', 'password' => '$2y$10$encrypted', 'name' => 'Admin User', // id, created_at, updated_at excluded ), )); } } ``` ## Filter Rows with WHERE Clause Export only rows matching specific criteria ```bash # Export only admin users php artisan iseed users --where="role = 'admin'" # Export active users with recent activity php artisan iseed users --where="active = 1 AND last_login > '2024-01-01'" # Export users from specific domain php artisan iseed users --where="email LIKE '%@example.com'" # Combine with max and orderby for precise control php artisan iseed users --where="status = 'verified'" --max=50 --orderby=id --direction=asc ``` ## Customize Seeder Class Names Add prefixes or suffixes to generated seeder class names ```bash # Add prefix to class name php artisan iseed users --classnameprefix=Backup # Generates: BackupUsersTableSeeder.php # Add suffix to class name php artisan iseed users --classnamesuffix=2024 # Generates: UsersTable2024Seeder.php # Useful for versioning or avoiding overwrites php artisan iseed users,posts --classnameprefix=Production # Generates: ProductionUsersTableSeeder.php, ProductionPostsTableSeeder.php ``` ## Force Overwrite Existing Seeds Automatically overwrite existing seeder files without prompting ```bash # Overwrite UsersTableSeeder.php if it exists php artisan iseed users --force # Overwrite multiple seeders php artisan iseed users,posts,comments --force # Useful for automated scripts and CI/CD pipelines ``` ## Configure Chunk Size Control insert statement size for large datasets ```bash # Use chunks of 100 rows per insert statement php artisan iseed products --chunksize=100 # Default chunk size is 500 (configurable in config/config.php) # Smaller chunks help avoid memory issues with large tables ``` ```php insert(array ( // ... 100 rows ... )); // Second chunk - 100 rows \DB::table('products')->insert(array ( // ... next 100 rows ... )); } } ``` ## Use Custom Database Connection Generate seeds from non-default database connection ```bash # Use 'mysql2' connection defined in config/database.php php artisan iseed users --database=mysql2 # Generate seeds from legacy database php artisan iseed legacy_users --database=legacy_db ``` ## Clean DatabaseSeeder Before Generation Clear previous seeder calls from DatabaseSeeder.php ```bash # Remove all previous calls between #iseed_start and #iseed_end php artisan iseed users --clean # Useful for regenerating entire seed suite php artisan iseed --clean ``` ## Generate Non-Indexed Array Seeds Create seeds with non-indexed arrays for merging ```bash # Generate seeder without array indices php artisan iseed users --noindex ``` ```php insert(array ( array ( 'email' => 'admin@example.com', 'name' => 'Admin', ), array ( 'email' => 'user@example.com', 'name' => 'User', ), )); } } ``` ## Add Pre-run and Post-run Events Execute Laravel events before or after seeding ```bash # Fire event before seeding php artisan iseed users --prerun=PrepareUserSeed # Fire event after seeding php artisan iseed users --postrun=UserSeededEvent # Multiple tables with different events php artisan iseed users,groups --prerun=PrepareUserSeed,PrepareGroupSeed --postrun=UserSeeded,GroupSeeded ``` ```php insert(array ( // ... data ... )); // Post-run event check $response = Event::until(new UserSeededEvent()); if ($response === false) { throw new Exception("Seed was executed but the postrun event failed!"); } } } ``` ## Use Programmatically via Facade Generate seeds from within Laravel application code ```php '/database/seeders', // Maximum number of rows per insert statement // Lower values help with memory usage on large tables 'chunk_size' => 500, ]; ``` ## Run Generated Seeds Execute generated seeders to populate database ```bash # Run all seeders defined in DatabaseSeeder.php php artisan db:seed # Run specific seeder class php artisan db:seed --class=UsersTableSeeder # Fresh migration with seeding php artisan migrate:fresh --seed # Refresh database and seed in one command php artisan migrate:refresh --seed ``` ## Summary iSeed simplifies the process of creating Laravel seed files by extracting data directly from existing database tables. The primary use cases include migrating data between development, staging, and production environments, creating realistic test fixtures from production data, and generating baseline seed data for new team members or deployment environments. The tool is especially valuable when working with legacy databases or when you need to capture the current state of a database for version control or documentation purposes. The package integrates with Laravel's existing seeding workflow and provides extensive filtering and customization options through command-line flags. Advanced features like WHERE clause filtering, column exclusion, custom chunk sizes, and pre/post-run events make it suitable for complex data migration scenarios. The ability to work programmatically via the Facade interface also enables integration into custom deployment scripts or data management tools, while the automatic DatabaseSeeder.php updates ensure generated seeds are immediately ready for use in the standard Laravel seeding workflow.