Download the PHP package bluekachina/seedfromjson without Composer
On this page you can find all versions of the php package bluekachina/seedfromjson. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bluekachina/seedfromjson
More information about bluekachina/seedfromjson
Files in bluekachina/seedfromjson
Package seedfromjson
Short Description Simplifies and standardizes seeding from JSON files named to match the table they will be populating
License MIT
Homepage https://github.com/Blue-Kachina/seedfromjson
Informations about the package seedfromjson
SeedFromJSON
Seed Your Database Using JSON files
This package makes it easy to have your DB seeded from JSON files. JSON files are expected to be named like the tables they contain the data for. This library was created mainly because of how easy it is to create JSON files from within the PHPStorm IDE
It supports PHP 7.3+
- Installation
- Configuration
- Storage Setup
- Usage
- Examples
- Quick Seeding
- Options
Installation
Require this package with composer using the following command:
Configuration
Publish the configuration for this package
|| ToDo: There's probably a more specific command you can use. I think you would need to name this package specifically.
Storage Setup
Create a disk in your config/filessystems.php
file.
Disks probably already exist for local
, public
, s3
, etc...
The name of the disk you create should match what is named within the config (default is seed_content
)
Usage
1) Create at least one JSON file within your seed_content
folder
2) Make use of this library from within one of your Laravel project's DatabaseSeeder.php
file.
3) Run php artisan migrate --seed
Examples
This library is registered as a singleton and so should be invoked using app(SeedFromJSON::class)
.
Step 1: Populate your seeding queue
Often times your tables will need to be populated in a specific sequence. Because of that, we make use of a queue so that you can still be in charge of that sequence. Simply add a new model to the seeding queue for each of the model/tables you want to populate
The method is defined with the following signature:
An example usage would be:
- When importing data, the system will look for a JSON file named the same way the model's table is named
Step 3: Begin seeding
Seeding takes place based on all the queued items that were just added
Quick Seeding
Quick seeding is enabled via config
/env
variable. See published config file
By. By defaultg, quick seeding is enabled only in local environments
When enabled, seeding imports a limited number of records (First x records from JSON).
The number of records (x) is also determined by config
/env
variable, and defaults to 100
Options
Many different options have been made available.
Each can be specified on a table-by-table basis, and since they're bitwise options, multiple can be used at a time (separated by |
).
Advanced Use
The beginSeeding function takes an optional parameter that determines whether or not seeding is actually ready to commence
Example
ExampleModelSeeder.php
Notice that we are including an optional parameter into the run
function, and then handing that off to the beginSeeding
function.
This is what allows us to process the seeding both as an individual seeder AND as part of a batch.
In order to take advantage of this as part of a batch you can do something like the following: DatabaseSeeder.php
Here we're telling all of the individual calls to defer. Once they're all done, we invoke beginSeeding
without deferring.