Package to create simple seeders with ability to lock/unlock tables
, (*1)
Overview
This package will allow you to write simple seeders:, (*2)
use SleepingOwl\Seeder\DataSeeder;
use SleepingOwl\Seeder\Seeder as SleepingOwlSeeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
# set global locale (default is en_US)
SleepingOwlSeeder::setDefaultLocale('de_DE');
# set global entries count (default is 10)
SleepingOwlSeeder::setDefaultTimes(10);
# truncate all tables before seeding (default is off)
SleepingOwlSeeder::truncateAll();
# seed Country model
SleepingOwlSeeder::model(Country::class)
->seed(function (DataSeeder $schema)
{
$schema->title->unique()->country;
});
# seed Company model
SleepingOwlSeeder::model(Company::class)
->seed(function (DataSeeder $schema)
{
$schema->title->unique()->company;
$schema->address->streetAddress;
$schema->phone->phoneNumber;
});
# seed Contact model
SleepingOwlSeeder::model(Contact::class)
->seed(function (DataSeeder $schema)
{
$schema->firstName->firstName;
$schema->lastName->lastName;
$schema->birthday->dateTimeThisCentury;
$schema->phone->phoneNumber;
$schema->address->address;
$schema->country_id->optional(0.9)->anyOf(Country::class);
$schema->comment->paragraph(5);
});
# seed company_contact table
SleepingOwlSeeder::table('company_contact')
->ignoreExceptions()
->seed(function ($schema)
{
$schema->company_id->anyOf(Company::class);
$schema->contact_id->anyOf(Contact::class);
});
}
}
And adds 2 new commands:, (*3)
-
php artisan seeder:lock <table> --all
— lock table from any changes (table data will be saved and restored after reseeding)., (*4)
-
php artisan seeder:unlock <table> --all
— unlock table for random seeding., (*5)
Installation
-
Require this packages in your composer.json and run composer update:, (*6)
"fzaninotto/faker": "1.5.*@dev",
"sleeping-owl/seeder": "1.*"
-
After composer update, add service providers to the config/app.php
, (*7)
'SleepingOwl\Seeder\SeederServiceProvider',
-
That's all., (*8)
Seeding
-
Import classes:, (*9)
use SleepingOwl\Seeder\DataSeeder;
use SleepingOwl\Seeder\Seeder as SleepingOwlSeeder;
-
Add seeding rule for table or model:, (*10)
SleepingOwlSeeder::table('table')->…
# or
SleepingOwlSeeder::model(\App\MyModel::class)->…
-
Configure seeding rule (you can configure it globally, see details in "Global Configuration"):, (*11)
SleepingOwlSeeder::table('table')
->truncate() # delete all data before seeding (default is off)
->locale('de_DE') # locale for this seed (default is en_US)
->times(100) # entities count to insert (default is 10)
->ignoreExceptions() # ignore query exceptions (default is off)
->...
-
Configure seeding schema (see details in "Schema Configuration"):, (*12)
SleepingOwlSeeder::table('table')
->…
->seed(function (DataSeeder $schema)
{
$schema->title->unique()->firstName;
$schema->country()->country;
$schema->field('my_field')->optional(0.9)->phoneNumber;
});
-
Now you can use default command php artisan db:seed
and new seeder:lock
/seeder:unlock
commands., (*13)
Global Configuration
You can configure seeding settings globally:, (*14)
SleepingOwlSeeder::setDefaultLocale('de_DE');
SleepingOwlSeeder::setDefaultTimes(100);
SleepingOwlSeeder::truncateAll();
SleepingOwlSeeder::setDefaultIgnoreExceptions(true);
This configuration will be used as default for every seeder. Seeder configuration will override global configuration:, (*15)
SleepingOwlSeeder::setDefaultTimes(100);
SleepingOwlSeeder::table('table')
->times(5) # this configuration has higher priority
->...
Schema Configuration
Add Field to the Schema
You must provide schema for seeding. There is 3 ways to add field to the schema:, (*16)
$schema->field('my_field')
$schema->my_field
$schema->my_field()
All these 3 ways will add field my_field
to the schema., (*17)
Provide Rules for Seeding the Field
You must use fzaninotto/faker package rules:, (*18)
# "phone" will be random phone number
# "->phone" is field name
# "->phoneNumber" is seeding rule
$schema->phone->phoneNumber;
# "my_field" will be unique sentence with 6 words
$schema->my_field->unique()->sentence(6);
# "country_title" will be country title in 90% cases and null in other 10%
$schema->country_title->optional(0.9)->country;
# "post_id" will be id of any \App\Post entity
$schema->post_id->anyOf(\App\Post::class);
# you can use your custom function for getting value for seeding
$schema->my_other_field->call(function ()
{
return mt_rand(0, 10);
})
# "int_field" will be random element from "$my_array"
$my_array = [1, 2, 3, 4];
$schema->int_field->randomElement($my_array);
Lock/Unlock Table Seeding
Locking
You can lock table seeding. This command will save table state and restores it with every seeding call., (*19)
You can lock one table:, (*20)
php artisan seeder:lock table_name
Or all tables:, (*21)
php artisan seeder:lock --all
Unlocking
This command will delete table saved state and restores default behaviour., (*22)
php artisan seeder:unlock table_name
or, (*23)
php artisan seeder:unlock --all
Support Library
You can donate via PayPal or in BTC: 13k36pym383rEmsBSLyWfT3TxCQMN2Lekd, (*24)
Copyright and License
Package was written by Sleeping Owl for the Laravel framework and is released under the MIT License. See the LICENSE file for details., (*25)