timokoerber / laravel-json-seeder
Create and use JSON files to seed your database in your Laravel applications
Installs: 28 703
Dependents: 0
Suggesters: 0
Security: 0
Stars: 46
Watchers: 3
Forks: 5
Open Issues: 1
Type:project
Requires (Dev)
- php: ^7.1.3
- ext-json: *
- illuminate/console: ^5.6|^6|^7|^8
- illuminate/database: ^5.6|^6|^7|^8
- illuminate/filesystem: ^5.6|^6|^7|^8
- illuminate/support: ^5.6|^6|^7|^8
This package is auto-updated.
Last update: 2024-12-24 00:21:23 UTC
README
Laravel JSON Seeder
Create and use JSON files to seed your database in your Laravel applications.
This package works both ways!
- Export your database into JSON files
- Use JSON files to seed your database
Installation
Require this package with composer. It is recommended to only require the package for development.
composer require timokoerber/laravel-json-seeder --dev
Next you need to publish the config file and register the required commands with ...
php artisan vendor:publish --provider="TimoKoerber\LaravelJsonSeeder\JsonSeederServiceProvider"
This will create the file config/jsonseeder.php
where you can find the configurations.
Next add the JsonSeederServiceProvider to the providers
array in config/app.php
...
// config/app.php 'providers' => [ ... TimoKoerber\LaravelJsonSeeder\JsonSeederServiceProvider::class, ... ]
Creating JSON seeds from database
Of course you can create the JSON files manually. But if you already have a good development database, you can easily export it into JSON seeds.
You can create seeds for every table in your database by calling ...
php artisan jsonseeds:create
This will create one JSON file for watch table in your database (i.e. table users -> users.json, table posts -> posts.json, etc.).
If you only want to create a seed of one specific table (i.e. users
), call ...
php artisan jsonseeds:create users
Existing files won't be overwritten by default. If you call the command again, a sub-directory will be created and the JSON seeds will be stored there.
If you want to overwrite the existing seeds, use the overwrite
option like ...
php artisan jsonseeds:create users -o|--overwrite
or just use the command ...
php artisan jsonseeds:overwrite users
Seeding
Go to your databas/seeds/DatabaseSeeder.php
and add the JsonSeeder inside the run()
method like this ...
// database/seeds/DatabaseSeeder.php class DatabaseSeeder extends Seeder { public function run() { $this->call(TimoKoerber\LaravelJsonSeeder\JsonDatabaseSeeder::class); } }
You can now call the JSON Seeder with the usual Artisan command ...
php artisan db:seed
Settings & Configurations
Directory
By default your seeds will be written into or read from the directory /database/json
. If you want a different directory, you can add the environment variable
JSON_SEEDS_DIRECTORY
in your .env
file ...
# .env
JSON_SEEDS_DIRECTORY=database/json
Ignoring tables
Some tables in your database might not require any seeds.
If you want to ignore these tables, you can put them into the setting ignore-tables
in the /config.jsonseeder.php
// config/jsonseeder.php 'ignore-tables' => [ 'migrations', 'failed_jobs', 'password_resets', ]
If a table in your database is empty, the LaravelJsonSeeder will create a JSON file with an empty array by default. This might be useful if you want your seeds to truncate this table.
If you don't want this, you can change the setting ignore-empty-tables
in config/jsonseeder.php
, so no JSON seed will be created.
// config/jsonseeder.php 'ignore-empty-tables' => true
Important!!! Do not forget to clear the cache after editing the config file:
php artisan cache:clear
Environments
The environment variable JSON_SEEDS_DIRECTORY
might be useful if you are using seeds in Unit Tests and want to use different seeds for this.
- database
- json
- development
- comapanies.json
- users.json
- posts.json
- testing
- users.json
- posts.json
Development
# .env
JSON_SEEDS_DIRECTORY=database/json/development
Testing
// phpunit.xml <phpunit> <php> <env name="JSON_SEEDS_DIRECTORY" value="database/json/testing"/> </php> </phpunit>
Errors & Warnings
License
Copyright © Timo Körber
Laravel JSON Seeder is open-sourced software licensed under the MIT license.