This package is built to reduce the time taken when seeding a lot data to the database on development.
You can install the package via composer:
composer require innoflash/steroid-seeder --dev
Used the same way we do with default Laravel factory
If you wanna use it in your existing seeding files just replace
factory
withsteroidFactory
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();
- 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();
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. |
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email innocentmazando@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.