iSeed

repository·master·Indexed 25 days ago

https://github.com/orangehill/iseed

A Laravel package that generates database seeders by inspecting existing data in database tables. It provides an `iseed` Artisan command and an `Iseed` facade to create snapshots of production or staging data for local development. Features include data filtering via `--where`, pagination with `--max` and `--skip`, custom class naming prefixes/suffixes, and support for MySQL foreign key checks and PostgreSQL sequence resets.

Tokens
2.4K
Snippets
11
Records
22
Agent score
84%

What's inside orangehill-iseed

  1. Define a custom iSeed template location in DatabaseSeeder

    master

    By default, iSeed adds calls to newly generated seeders into DatabaseSeeder.php. You can control exactly where these calls are placed by wrapping a section in your DatabaseSeeder.php with #iseed_start and #iseed_end markers.

    public function run()
    {
        #iseed_start
    
        // All iSeed generated seeder calls will be placed here.
    
        #iseed_end
    }
  2. Install iSeed via Composer

    master

    To install iSeed in a Laravel project, use Composer. For modern Laravel versions (8, 9, 10, 11, 12, and 13), use the standard command. For older versions, specific versions are required.

    Laravel 8-13 (PHP 8.0+):

    composer require orangehill/iseed

    Laravel 5.3.7 and below:

    composer require orangehill/iseed:2.2

    Laravel 4:

    composer require orangehill/iseed:1.1
    composer require orangehill/iseed
  3. Generate seeders using the `iseed` Artisan command

    master

    The iseed command generates new seed files based on existing database table data. You can target specific tables or generate seeders for all tables in your database.

    Generate for all tables:

    php artisan iseed

    Generate for a single table:

    php artisan iseed my_table

    Generate for multiple tables (CSV notation):

    php artisan iseed my_table,another_table
    php artisan iseed my_table
  4. Register iSeed Service Provider (Laravel 5.4 and below)

    master

    If you are using Laravel 5.4 or below, you must manually add the service provider to the providers array in your /app/config/app.php file to enable auto-discovery.

    Orangehill\Iseed\IseedServiceProvider::class,
  5. Handle Foreign Key constraints and PostgreSQL sequences

    master

    Use these options to prevent errors during the seeding process:

    • MySQL Foreign Keys: Use --skip-fk-checks to wrap the seeder in SET FOREIGN_KEY_CHECKS statements.
    • PostgreSQL Sequences: Use --reset-sequences to include statements that reset sequences to the maximum ID value, preventing duplicate key errors on subsequent inserts.
    php artisan iseed users --skip-fk-checks
    php artisan iseed users --reset-sequences
  6. Customize the seed file stub template

    master

    You can define a custom stub for the generated seeder files.

    1. Create a config/iseed.php file to define your stub directory:
    return [
        'stub_path' => resource_path('stubs'),
    ];
    1. Create your custom stub at resources/stubs/seed.stub.

    Available Placeholders:

    • {{class}}: The seeder class name.
    • {{table}}: The database table name.
    • {{insert_statements}}: The generated insert statements.
    • {{prerun_event}}: Pre-run event code (if specified).
    • {{postrun_event}}: Post-run event code (if specified).
    // config/iseed.php
    return [
        'stub_path' => resource_path('stubs'),
    ];
  7. Configure the insert chunk size

    master
    To prevent errors when seeding tables with a very large number of records, you can adjust the chunk_size in the iSeed configuration file. This determines the maximum number of rows included in a single insert statement.
  8. Filter seeded data with the `--where` option

    master

    Use the --where option to pass a raw SQL WHERE clause to filter which rows are included in the generated seeder. Ensure you properly escape the string for your shell.

    Example: Seed users with specific email patterns:

    php artisan iseed users --where="email LIKE '%@example.com'"
    php artisan iseed users --where="email LIKE '%@example.com'"
  9. Customize Seeder class names with prefixes and suffixes

    master

    Use --classnameprefix or --classnamesuffix to create additional seeders without overwriting existing ones.

    • --classnameprefix=Customized: Outputs CustomizedMyTableSeeder.php.
    • --classnamesuffix=Customizations: Outputs MyTableCustomizationsSeeder.php.
    php artisan iseed my_table --classnameprefix=Customized
  10. Paginate large tables using `--skip`, `--max`, and `--orderby`

    master

    To export large tables in chunks, combine --max, --skip, and --orderby.

    Example: Exporting pages of 1000 rows:

    # Page 1
    php artisan iseed users --max=1000 --orderby=id
    
    # Page 2
    php artisan iseed users --max=1000 --skip=1000 --orderby=id
    
    # Page 3
    php artisan iseed users --max=1000 --skip=2000 --orderby=id
    php artisan iseed users --max=1000 --skip=1000 --orderby=id
  11. Generate seed files using the Iseed facade

    master

    You can programmatically generate a seed file for a specific table using the \Iseed::generateSeed method. This creates a new seeder file in /database/seeders and automatically updates DatabaseSeeder.php to include the new class.

    Method Signature: \Iseed::generateSeed(string $tableName, string $connectionName = null, int $numOfRows = null);

    • $tableName: The name of the table to seed.
    • $connectionName (optional): The database connection to use.
    • $numOfRows (optional): The number of rows to export.
    \Iseed::generateSeed('users', 'connectionName', 'numOfRows');