tyghaykal/laravel-seed-generator

Laravel Seed Generator

v2.0.5 2024-11-12 18:08 UTC

README

Test CI Latest Stable Version Total Downloads Latest Unstable Version License

Generate laravel seeder file from a real data from your database.

Supported Database

  • MariaDB
  • MySQL
  • PostgreSQL
  • SQL Server
  • SQLite

Version Compatibility

Install

composer require --dev tyghaykal/laravel-seed-generator

Laravel Setup

Laravel will automatically register the service provider for you, so no more step on this.

You can generate a config file to personalize this package using vendor:publish and choose TYGHaykal\LaravelSeedGenerator\SeedGeneratorServiceProvider.

php artisan vendor:publish

In the config file, you can adjust some of settings on generating seeders file such as namespace, prefix, suffix, and connection.

Usage

Options

Usage on Model with Option

Just set the model value with your model Namespace without \App\Models or \App or your defined namespace on config.php, for example your model namespace is under \App\Models\Master\Type, so you just need type Master\Type. Can be multiple and separated with comma.

php artisan seed:generate --model-mode --models=Master\Type

You can run the command and show the menu using

php artisan seed:generate --model-mode --models=Master\Type --show-prompt

You can filter which data will be included into seeder file using where clause

php artisan seed:generate --model-mode --models=Master\Type --where=field,type,value

You can filter which data will be included into seeder file using where in clause

php artisan seed:generate --model-mode --models=Master\Type --where-in=field,value1,value2

You can filter which data will not be included into seeder file using where not in clause

php artisan seed:generate --model-mode --models=Master\Type --where-in=field,value1,value2

You can order the data will be included into seeder file using order-by clause

php artisan seed:generate --model-mode --models=Master\Type --order-by=field,type

You can limit the data will be included into seeder file using limit clause

php artisan seed:generate --model-mode --models=Master\Type --limit=10

You can also define which data you want to include to seeder file based on the id with, can only run with single model only:

php artisan seed:generate --model-mode --models=Master\Type --ids="1,2,3"

Or you want to skip some ids:

php artisan seed:generate --model-mode --models=Master\Type --ignore-ids="1,2,3"

You can also define which field that you want include to seeder file based on the field name with:

php artisan seed:generate --model-mode --models=Master\Type --fields="id,name"

Or you want skip some fields:

php artisan seed:generate --model-mode --models=Master\Type --ignore-fields="id,name"

You can also define which hasMany relation that you want seed, only has effect on model mode with single model only:

php artisan seed:generate --model-mode --models=Master\Type --relations="relationName1,relationName2"

You can also limit the relation that you want seed, only has effect on model mode with single model only:

php artisan seed:generate --model-mode --models=Master\Type --relations="relationName1,relationName2" --relations-limit=10

You can also change the location of generated seeder file:

php artisan seed:generate --model-mode --models=Master\Type --output=Should/Be/In/Here/Data

// it will produce in path database/seeders/Should/Be/In/Here/DataSeeder
// or
// it will produce in path database/seeds/Should/Be/In/Here/DataSeeder

By default, every generated seeders file will be presented on DatabaseSeeder.php, if you don't want it, you can use --no-seed option:

php artisan seed:generate --model-mode --models=Master\Type --no-seed

Usage on Table with Option

Just set the tables value as table name, can be multiple separated with comma

php artisan seed:generate --table-mode --tables=master_leave_types

You can run the command and show the menu using

php artisan seed:generate --table-mode --tables=master_leave_types --show-prompt

You can filter which data will be included into seeder file using where clause

php artisan seed:generate --table-mode --tables=master_leave_types --where=field,type,value

You can filter which data will be included into seeder file using where in clause

php artisan seed:generate --table-mode --tables=master_leave_types --where-in=field,value1,value2

You can filter which data will not be included into seeder file using where not in clause

php artisan seed:generate --table-mode --tables=master_leave_types --where-in=field,value1,value2

You can order the data will be included into seeder file using order-by clause

php artisan seed:generate --table-mode --tables=master_leave_types --order-by=field,type

You can limit the data will be included into seeder file using limit clause

php artisan seed:generate --table-mode --tables=master_leave_types --limit=10

You can also define which data you want to include to seeder file based on the id with:

php artisan seed:generate --table-mode --tables=master_leave_types --ids="1,2,3"

Or you want to skip some ids:

php artisan seed:generate --table-mode --tables=master_leave_types --ignore-ids="1,2,3"

You can also define which field that you want include to seeder file based on the field name with:

php artisan seed:generate --table-mode --tables=master_leave_types --fields="id,name"

Or you want skip some fields:

php artisan seed:generate --table-mode --tables=master_leave_types --ignore-fields="id,name"

You can also change the location of generated seeder file:

php artisan seed:generate --table-mode --tables=master_leave_types --output=Should/Be/In/Here/Data

// it will produce in path database/seeders/Should/Be/In/Here/DataSeeder
// or
// it will produce in path database/seeds/Should/Be/In/Here/DataSeeder

By default, every generated seeders file will be presented on DatabaseSeeder.php, if you don't want it, you can use --no-seed option:

php artisan seed:generate --table-mode --tables=master_leave_types --no-seed