Download the PHP package mathieutu/laravel-json-syncer without Composer
On this page you can find all versions of the php package mathieutu/laravel-json-syncer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mathieutu/laravel-json-syncer
More information about mathieutu/laravel-json-syncer
Files in mathieutu/laravel-json-syncer
Package laravel-json-syncer
Short Description Export and import your Eloquent models to/from JSON
License MIT
Informations about the package laravel-json-syncer
Laravel Json Syncer: Json importer and exporter for Laravel
Installation
Require this package in your composer.json and update composer.
Add the JsonExportable
and/or JsonImportable
interfaces and JsonExporter
and/or JsonImporter
traits to your models.
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 theHasOneOrMany
relations of your model. You can change that by setting the$jsonExportableAttributes
and$jsonExportableRelations
properties or overwriting thegetJsonExportableAttributes()
andgetJsonExportableRelations()
methods. - JsonImporter:
By default, it will import all the attributes which are in the
$fillable
properties and all theHasOneOrMany
relations of your model. You can change that by setting the$jsonImportableAttributes
and$jsonImportableRelations
properties or overwriting thegetJsonImportableAttributes()
andgetJsonImportableRelations()
methods.
Usage
Use the $model->exportToJson($jsonOptions = 0)
to export the object, all its attributes and its children (via its relations).
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 :
We can export it by:
It will return:
How to import
And exactly the same for the opposite. We can import the json returned by the previous method, or another one. For the exact same app If we want to import this new very simple set of data:
We can import it with:
And it will create all the entities in database:
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.
All versions of laravel-json-syncer with dependencies
illuminate/support Version ^v10.22.0
illuminate/database Version ^v10.22.0
ext-json Version *