innoflash/steroid-seeder

A Laravel package to speed up seeding in Laravel.

v1.1.1 2020-06-27 14:05 UTC

This package is auto-updated.

Last update: 2024-05-27 23:15:28 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

This package is built to reduce the time taken when seeding a lot data to the database on development.

carbon.png

Installation

You can install the package via composer:

composer require innoflash/steroid-seeder --dev

Usage

Used the same way we do with default Laravel factory

If you wanna use it in your existing seeding files just replace factory with steroidFactory

Alternatively you can call the factory from the facade. SteroidSeeder::factory

Default factory seeds one model at a time and that elongates the time of execution for huge data-sets.

// with default factory (approx 37 seconds on my computer)

factory(TestModel::class, 1000)->create();
//with steroidFactory (approx 8 seconds on my machine)

steroidFactory(\App\TestModel::class, 1000)->create();
Steroid seeder optimization.
  • By default the steroidFactory save 1000 entries at a go, you cant tune this to whatever size that works for you.
// took 8.8 seconds to seed 10k entries

steroidFactory(\App\TestModel::class, 100000)
    ->chunk(1000)
    ->create();
  • By default the Laravel factory calls some callbacks after creating your models. This is when the model boot and observers are called and its time consuming because the factory iterate over all the created models. steroidFactory lets you ignore the callbacks.
// took 4.3 seconds to seed 10k entries

steroidFactory(\App\TestModel::class, 100000)
    ->skipAfterCreatingCallbacks()
    ->create();

Seeding relationships

Steroid seeder can be used to create models with their relationships.

It's a continuation of the above except that the you will need to chain your relationships on the factory. See the example below:

    steroidFactory(TestModel::class)
            ->with(Comment::class)
            ->with(Reaction::class, 1, [], 'model_id')
            ->create();

Here is the params expected in the with function.

Position Variable Type Required Default Description
1 $class string true null The class name of the model to be related with the model in create.
2 $size int false 1 The number of models to be seeded with the parent increate.
3 $attributes array false [] The default attributes to set to the models.
4 $foreignKeyName string false parent key The name of the parent's foreign key column name in the models to be seeded.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email innocentmazando@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.