This is my fork of orangehill/iseed. I updated a few things and added iseed:all command.
Inverse seed generator (Iseed) is a Laravel package that provides a method to generate a new seed file based on data from the existing database table.
1. Require with Composer
$ composer require SchuBu/iseed
Laravel 5.3.7 and below or Laravel 4 need specific version.
$ composer require SchuBu/iseed:2.2 # Laravel 5.3.7 and below
$ composer require SchuBu/iseed:1.1 # Laravel 4
Latest Laravel versions have auto dicovery and automatically add service provider - if you're using 5.4.x and below, remember to add it to providers
array at /app/config/app.php
:
// ...
SchuBu\Iseed\IseedServiceProvider::class,
Iseed comes with two artisan commands:
php artisan iseed
php artisan iseed:all
$ php artisan iseed --help
Description:
Generate seed file from table
Usage:
iseed [options] [--] <tables>
Arguments:
tables comma separated string of table names
Options:
--clean clean iseed section
--force force overwrite of all existing seed classes
--database[=DATABASE] database connection [default: "mysql"]
--max[=MAX] max number of rows
--chunksize[=CHUNKSIZE] size of data chunks for each insert query
--exclude[=EXCLUDE] exclude columns
--prerun[=PRERUN] prerun event name
--postrun[=POSTRUN] postrun event name
--dumpauto[=DUMPAUTO] run composer dump-autoload [default: true]
--noindex no indexing in the seed
--orderby[=ORDERBY] orderby desc by column
--direction[=DIRECTION] orderby direction
--classnameprefix[=CLASSNAMEPREFIX] prefix for class and file name
--classnamesuffix[=CLASSNAMESUFFIX] suffix for class and file name
$ php artisan iseed:all --help
Description:
Generate seed files for all tables except migrations
Usage:
iseed:all [options]
Options:
--force
The primary artisan command (php artisan iseed
) contains a series of arguments (some required, some optional) you can use when calling the command.
Mandatory parameter which defines which table/s will be used for seed creation. Use CSV notation for multiple tables. Seed file will be generated for each table.
Examples:
$ php artisan iseed my_table
$ php artisan iseed my_table,another_table
Optionally specify a prefix or suffix for the Seeder class name and file name. This is useful if you want to create an additional seed for a table that has an existing seed without overwriting the existing one.
Examples:
$ php artisan iseed my_table --classnameprefix=Customized
outputs CustomizedMyTableSeeder.php
$ php artisan iseed my_table,another_table --classnameprefix=Customized
outputs CustomizedMyTableSeeder.php
and CustomizedAnotherTableSeeder.php
$ php artisan iseed my_table --classnamesuffix=Customizations
outputs MyTableCustomizationsSeeder.php
$ php artisan iseed my_table,another_table --classnamesuffix=Customizations
outputs MyTableCustomizationsSeeder.php
and AnotherTableCustomizationsSeeder.php
Optional parameter which is used to automatically overwrite any existing seeds for desired tables.
Example:
The following command will overwrite UsersTableSeeder.php
if it already exists in Laravel's seeds directory.
$ php artisan iseed users --force
Optional boolean parameter that controls the execution of composer dump-autoload
command. Defaults to true
.
Example that will stop composer dump-autoload
from execution:
$ php artisan iseed users --dumpauto=false
Optional parameter which will clean app/database/seeders/DatabaseSeeder.php
before creating new seed class.
Example:
$ php artisan iseed users --clean
Optional parameter which specifies the DB connection name.
Example:
$ php artisan iseed users --database=mysql2
Optional parameter which defines the maximum number of entries seeded from a specified table. In case of multiple tables, limit will be applied to all of them.
Example:
$ php artisan iseed users --max=10
Optional parameter which defines the size of data chunks for each insert query.
Example:
$ php artisan iseed users --chunksize=100
Please note that some users encountered a problem with large DB table exports. The issue is solved by splitting input data into smaller chunks of elements per insert statement. You may need to change the chunk size value in some extreme cases where a DB table has a large number of records, the chunk size is configurable in Iseed's config.php
file or via the artisan
command.
Optional parameter which defines the column which will be used to order the results by, when used in conjunction with the max parameter that allows you to set the desired number of exported database entries.
Example:
$ php artisan iseed users --max=10 --orderby=id
Optional parameter which allows you to set the direction of the ordering of results; used in conjuction with orderby parameter.
Example:
$ php artisan iseed users --max=10 --orderby=id --direction=desc
Optional parameter which accepts comma separated list of columns that you'd like to exclude from tables that are being exported. In case of multiple tables, exclusion will be applied to all of them.
Example:
$ php artisan iseed users --exclude=id
$ php artisan iseed users --exclude=id,created_at,updated_at
Optional parameter which assigns a laravel event name to be fired before seeding takes place. If an event listener returns false
, seed will fail automatically.
You can assign multiple preruns for multiple table names by passing an array of comma separated DB names and respectively passing a comma separated array of prerun event names.
Example:
The following command will make a seed file which will fire an event named someEvent
before seeding takes place.
$ php artisan iseed users --prerun=someEvent
The following example will assign someUserEvent
to users
table seed, and someGroupEvent
to groups
table seed, to be executed before seeding.
$ php artisan iseed users,groups --prerun=someUserEvent,someGroupEvent
The following example will only assign a someGroupEvent
to groups
table seed, to be executed before seeding. Value for the users table prerun was omitted here, so users
table seed will have no prerun event assigned.
$ php artisan iseed users,groups --prerun=,someGroupEvent
Optional parameter which assigns a laravel event name to be fired after seeding takes place. If an event listener returns false
, seed will be executed, but an exception will be thrown that the postrun failed.
You can assign multiple postruns for multiple table names by passing an array of comma separated DB names and respectively passing a comma separated array of postrun event names.
Example:
The following command will make a seed file which will fire an event named someEvent
after seeding was completed.
$ php artisan iseed users --postrun=someEvent
The following example will assign someUserEvent
to users
table seed, and someGroupEvent
to groups
table seed, to be executed after seeding.
$ php artisan iseed users,groups --postrun=someUserEvent,someGroupEvent
The following example will only assign a someGroupEvent
to groups
table seed, to be executed after seeding. Value for the users table postrun was omitted here, so users
table seed will have no postrun event assigned.
$ php artisan iseed users,groups --postrun=,someGroupEvent
By using --noindex
the seed can be generated as a non-indexed array.
The use case for this feature is when you need to merge two seed files.
Example:
$ php artisan iseed users --noindex
Iseed comes with the following configuration, to change the default first publish the configuration with:
$ php artisan vendor:publish --provider="SchuBu\Iseed\IseedServiceProvider" --tag="config"
Path where the seeders will be generated.
The default is /database/seeders
.
Path where the Seeder file is saved.
The default is /database/seeders/DatabaseSeeder.php
Whether the Seeder should be modified after running the iseed
command.
The default is true
.
Maximum number of rows per insert statement.
The default is 500
.
You may alternatively set a relative path to a custom stub file. Make sure to make path relative to your project root, e.g. 'stub_path' => 'stubs/seeder.stub'
or 'stub_path' => './stubs/seeder.stub'
but not 'stub_path' => '/stubs/seeder.stub'
.
The default stub file is located in /vendor/schubu/iseed/src/SchuBu/Iseed/Stubs/seed.stub
You may customize the line that preceeds the inserts inside the seeder.
You MUST keep both %s
however, the first will be replaced by the table name and the second by the inserts themselves.
The default is \DB::table('%s')->insert(%s);
.
To generate a seed file for your individual tables simply call:
\Iseed::generateSeed($tableName);
$tableName
needs to define the name of your table. There are also parameters you can pass to the generateSeed()
method which include:
Variable | Description | Default | Type | Required |
---|---|---|---|---|
$tableName | Table name | string | ✅ | |
$prefix | Seeder class prefix | null | string | ❌ |
$suffix | Seeder class suffix | null | string | ❌ |
$database | Database connection name | null | string | ❌ |
$max | Maximum seeded entries | 0 | integer | ❌ |
$chunkSize | Size of data chunks | 0 | integer | ❌ |
$exclude | Columns to exclude | null | string | ❌ |
$prerunEvent | Prerun event name | null | string | ❌ |
$postrunEvent | Postrun event name | null | string | ❌ |
$dumpAuto | Composer auto-dump | true | string | ❌ |
$indexed | Indexed array | true | string | ❌ |
$orderBy | Column to order by | null | string | ❌ |
$direction | Default sort order | ASC | string | ❌ |
For example, to generate a seed for your users
table you would call:
\Iseed::generateSeed('users', 'mysql2', 100);
This will create a file inside the /database/seeders
folder (/database/seeds
for Laravel 5 to 7 and /app/database/seeds
for Laravel 4) called UsersTableSeeder.php
with the contents similar to following example:
<?php
// File: /database/seeders/UsersTableSeeder.php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder {
/**
* Auto generated seeder file.
*
* @return void
*/
public function run()
{
\DB::table('users')->delete();
\DB::table('users')->insert(array (
0 =>
array (
'id' => '1',
'email' => '[email protected]',
'password' => '$2y$10$tUGCkQf/0NY3w1l9sobGsudt6UngnoVXx/lUoh9ElcSOD0ERRkK9C',
'permissions' => NULL,
'activated' => '1',
'activation_code' => NULL,
'activated_at' => NULL,
'last_login' => NULL,
'persist_code' => NULL,
'reset_password_code' => NULL,
'first_name' => NULL,
'last_name' => NULL,
'created_at' => '2013-06-11 07:47:40',
'updated_at' => '2013-06-11 07:47:40',
),
1 =>
array (
'id' => '2',
'email' => '[email protected]',
'password' => '$2y$10$ImNvsMzK/BOgNSYgpjs/3OjMKMHeA9BH/hjl43EiuBuLkZGPMuZ2W',
'permissions' => NULL,
'activated' => '1',
'activation_code' => NULL,
'activated_at' => NULL,
'last_login' => '2013-06-11 07:54:57',
'persist_code' => '$2y$10$C0la8WuyqC6AU2TpUwj0I.E3Mrva8A3tuVFWxXN5u7jswRKzsYYHK',
'reset_password_code' => NULL,
'first_name' => NULL,
'last_name' => NULL,
'created_at' => '2013-06-11 07:47:40',
'updated_at' => '2013-06-11 07:54:57',
),
));
}
}
This command will also update /database/seeders/DatabaseSeeder.php
(/database/seeds/DatabaseSeeder.php
for Laravel 5 to 7 and /app/database/seeds/DatabaseSeeder.php
for Laravel 4) to include a call to this newly generated seed class.
If you wish you can define a custom Iseed template in which all the calls will be placed. You can do this by using #iseed_start
and #iseed_end
templates anywhere within /database/seeders/DatabaseSeeder.php
(/database/seeds/DatabaseSeeder.php
for Laravel 5 to 7 and /app/database/seeds/DatabaseSeeder.php
for Laravel 4), for example:
<?php
// File: /database/seeders/DatabaseSeeder.php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
if(App::environment() == "local")
{
throw new \Exception('Only run this from production');
}
// All generated seeds are added between #iseed_start & #iseed_end.
#iseed_start
$this->call(UsersTableSeeder::class);
#iseed_end
}
}
Alternatively you can run Iseed from the command line using Artisan, e.g. php artisan iseed users
. For generation of multiple seed files comma separated list of table names should be send as an argument for command, e.g. php artisan iseed users,posts,groups
.
In case you try to generate a seed file that already exists the command will ask you a question whether you want to overwrite it or not. If you wish to overwrite it by default use the --force
artisan command option, e.g. php artisan iseed users --force
.
If you wish to clear the Iseed template you can use the --clean
artisan command option, e.g. php artisan iseed users --clean
. This will clean the template from database/seeders/DatabaseSeeder.php
before creating the new seed class.
You can specify a db connection that will be used for creation of new seed files by using the --database=connection_name
artisan command option, e.g. php artisan iseed users --database=mysql2
.
To limit the number of rows that will be exported from table use the --max=number_of_rows
artisan command option, e.g. php artisan iseed users --max=10
. If you use this option while exporting multiple tables the specified limit will be applied to all of them.