PHPackages                             bluekachina/seedfromjson - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. bluekachina/seedfromjson

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

bluekachina/seedfromjson
========================

Simplifies and standardizes seeding from JSON files named to match the table they will be populating

1.2.3(2y ago)22.1k—0%MITPHPPHP &gt;=7.3.0

Since Dec 14Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Blue-Kachina/seedfromjson)[ Packagist](https://packagist.org/packages/bluekachina/seedfromjson)[ Docs](https://github.com/Blue-Kachina/seedfromjson)[ RSS](/packages/bluekachina-seedfromjson/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (15)Used By (0)

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](#installation)
- [Configuration](#configuration)
- [Storage Setup](#storage-setup)
- [Usage](#usage)
- [Examples](#examples)
- [Quick Seeding](#quick-seeding)
- [Options](#options)

Installation
------------

[](#installation)

Require this package with composer using the following command:

```
composer require bluekachina/seedfromjson
```

Configuration
-------------

[](#configuration)

Publish the configuration for this package

```
php artisan vendor:publish --tag=config
```

|| ToDo: There's probably a more specific command you can use. I think you would need to name this package specifically.

Storage Setup
-------------

[](#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`)

```
    'seed_content' => [
        'driver' => 'local',
        'root' => database_path().'/seeders/seed_content',
    ],

```

Usage
-----

[](#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
--------

[](#examples)

This library is registered as a singleton and so should be invoked using `app(SeedFromJSON::class)`.

### Step 1: Populate your seeding queue

[](#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:

```
function addModelToSeedingQueue($model, $options = 0, $callback_pre = null, $callback_post = null);

```

An example usage would be:

```
app(SeedFromJSON::class)->addModelToSeedingQueue(User::class, OPT_TRUNCATE_TABLE | OPT_IMPORT_DATA );

```

- *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

[](#step-3-begin-seeding)

Seeding takes place based on all the queued items that were just added

```
app(SeedFromJSON::class)->beginSeeding();

```

Quick Seeding
-------------

[](#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
-------

[](#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 `|`).

```
OPT_TRUNCATE_TABLE          => Determines whether or not to truncate the table connected to the model provided
OPT_IMPORT_DATA             => Determines if JSON file (filename matches tablename) is to be imported from
OPT_SKIP_PK                 => Determines if the PK field's value is to be preserved
OPT_DISABLE_FK_CONSTRAINTS  => Determines if foreign key constraints should be disabled prior to importing JSON data
OPT_NO_SEED_IN_PROD         => Determines if table is to be populated in a development environment only
OPT_NO_TRUNCATE_IN_PROD     => Determines if table is to be truncated in a development environment only
OPT_ALWAYS_FULL_SEED        => Determines if table is to be fully seeded -- even if running a quick seed

```

Advanced Use
------------

[](#advanced-use)

The beginSeeding function takes an optional parameter that determines whether or not seeding is actually ready to commence

```
function beginSeeding($defer=false);

```

### Example

[](#example)

*ExampleModelSeeder.php*

```
