All Projects → mathieutu → laravel-json-syncer

mathieutu / laravel-json-syncer

Licence: MIT License
A Json importer and exporter for Laravel.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-json-syncer

Trakt2Letterboxd
Script to export your movies from Trakt to Letterboxd
Stars: ✭ 27 (+22.73%)
Mutual labels:  importer, exporter
Mod3-MHW-Importer
Blender Mod3 Import-Exporter for Monster Hunter World
Stars: ✭ 44 (+100%)
Mutual labels:  importer, exporter
daru-io
daru-io is a plugin gem to the existing daru gem, which aims to add support to Importing DataFrames from / Exporting DataFrames to multiple formats.
Stars: ✭ 21 (-4.55%)
Mutual labels:  importer, exporter
userstamps
A simple package to insert and load userstamps for a model automatically, it provides an eloquent trait to use in models..
Stars: ✭ 34 (+54.55%)
Mutual labels:  eloquent
opentsdb exporter
Prometheus exporter for OpenTSDB
Stars: ✭ 20 (-9.09%)
Mutual labels:  exporter
jail exporter
A Prometheus exporter for FreeBSD jail metrics
Stars: ✭ 21 (-4.55%)
Mutual labels:  exporter
laravel-boolean-dates
Automatically convert Eloquent model boolean attributes to dates (and back).
Stars: ✭ 31 (+40.91%)
Mutual labels:  eloquent
beancount-plugins
A collection of my custom beancount importers & price sources, written in Python
Stars: ✭ 14 (-36.36%)
Mutual labels:  importer
Jekyll
Call of Duty XAsset exporter that dumps raw assets from a game's memory.
Stars: ✭ 29 (+31.82%)
Mutual labels:  exporter
laravel-loggable
🎥 📽 🎞 Log your model changes in multiple ways
Stars: ✭ 58 (+163.64%)
Mutual labels:  eloquent
eloquent-has-by-non-dependent-subquery
Convert has() and whereHas() constraints to non-dependent subqueries.
Stars: ✭ 70 (+218.18%)
Mutual labels:  eloquent
MidiAnimImporter
A custom importer that imports a .mid file (SMF; Standard MIDI File) into an animation clip.
Stars: ✭ 69 (+213.64%)
Mutual labels:  importer
selectel-exporter
No description or website provided.
Stars: ✭ 25 (+13.64%)
Mutual labels:  exporter
airflow-prometheus-exporter
Export Airflow metrics (from mysql) in prometheus format
Stars: ✭ 25 (+13.64%)
Mutual labels:  exporter
eloquent-traits
Traits for laravel eloquent models
Stars: ✭ 18 (-18.18%)
Mutual labels:  eloquent
authorized-attributes
Authorized Model Attributes for Laravel
Stars: ✭ 22 (+0%)
Mutual labels:  eloquent
docker-pulls
A simple compose script to use in conjunction with the prometheus stack to monitor Docker pulls.
Stars: ✭ 19 (-13.64%)
Mutual labels:  exporter
eloquent-phpunit
Eloquent model and database schema PHPUnit test case
Stars: ✭ 20 (-9.09%)
Mutual labels:  eloquent
model-observers
Make model observers easy
Stars: ✭ 17 (-22.73%)
Mutual labels:  eloquent
Tile-Studio
Tile / Sprite / Map Editor
Stars: ✭ 59 (+168.18%)
Mutual labels:  exporter

Laravel Json Syncer: Json importer and exporter for Laravel

Github checks Test coverage Code quality Packagist downloads Stable version

Installation

Require this package in your composer.json and update composer.

composer require mathieutu/laravel-json-syncer

Just add the JsonExportable and/or JsonImportable interfaces and JsonExporter and/or JsonImporter traits to your models.

namespace App\Models;

//...
use MathieuTu\JsonSyncer\Contracts\JsonExportable;
use MathieuTu\JsonSyncer\Traits\JsonExporter;
// or/and
use MathieuTu\JsonSyncer\Contracts\JsonImportable;
use MathieuTu\JsonSyncer\Traits\JsonImporter;

class User extends Model implements JsonExportable, JsonImportable
{
    use JsonExporter;
    use JsonImporter;
    // ...
}

No service providers required!

Configuration

Out of the box, the Importer and Exporter will automatically guess what attributes and relations to handle, but you can customize everything:

  • JsonExporter: By default, it will export all the attributes in the $fillable properties, except those with *_id pattern, and all the HasOneOrMany relations of your model. You can change that by setting $jsonExportableAttributes and $jsonExportableRelations properties or overwriting getJsonExportableAttributes() and getJsonExportableRelations() methods.

  • JsonImporter: By default, it will import all the attributes which are in the $fillable properties and all the HasOneOrMany relations of your model. You can change that by setting $jsonImportableAttributes and $jsonImportableRelations properties or overwriting getJsonImportableAttributes() and getJsonImportableRelations() methods.

Usage

Just use the $model->exportToJson($jsonOptions = 0) to export the object and all its attributes and children.

Use Model::importFromJson($objectsToCreate) to import a json string or its array version.

Examples

(You can find all this examples in package tests)

How to export

If we consider this dataset in database :

{
    "foos (Foo models)": [{
        "id": 1,
        "author": "Mathieu TUDISCO",
        "username": "@mathieutu",
        "bars (HasMany relation with Bar models)": [
            {
                "id": 1,
                "name": "bar1",
                "foo_id": "1",
                "baz (HasOne relation with Baz model)": {
                    "id": 1,
                    "name": "bar1_baz",
                    "bar_id": "1"
                }
            },
            {
                "id": 2,
                "name": "bar2",
                "foo_id": "1",
                "baz (HasOne relation with Baz model)": {
                    "id": 2,
                    "name": "bar2_baz",
                    "bar_id": "2"
                }
            }
        ]
    }]
}

We can export it by:

Foo::first()->exportToJson(JSON_PRETTY_PRINT);

It will return:

{
    "author": "Mathieu TUDISCO",
    "username": "@mathieutu",
    "bars": [
        {
            "name": "bar1",
            "baz": {
                "name": "bar1_baz"
            }
        },
        {
            "name": "bar2",
            "baz": {
                "name": "bar2_baz"
            }
        }
    ]
}

How to import

And exactly the same for the opposite. We can import the json returned by the previous method, or an other one. For the exact same app If we want to import this new very simple set of data:

{
    "username": "@mathieutu",
    "bars": {
        "name": "my unique simple bar!"
    }
}

We can import it with:

Foo::importFromJson($json);

And it will create all the entities in database:

dd(Foo::with('bars.baz')->first()->toArray());
/*
array:4 [
  "id" => 1
  "author" => null
  "username" => "@mathieutu"
  "bars" => array:1 [
    0 => array:4 [
      "id" => 1
      "name" => "my unique simple bar!"
      "foo_id" => "1"
      "baz" => null
    ]
  ]
]
*/

License

This JSON Syncer for Laravel is an open-sourced software licensed under the MIT license.

Contributing

Issues and PRs are obviously welcomed, as well for new features than documentation. Each piece of code added should be fully tested, but we can do that all together, so please don't be afraid by that.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].