https://packalyst.com/packages/package/ ... son-syncer
Laravel Json Syncer: Json importer and exporter for Laravel
Travis build StyleCI 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.
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.
Laravel Json Syncer: Json importer and exporter for Laravel
前往
- Software
- ↳ CodeCharge Studio
- ↳ CodeCharge
- ↳ DemoCharge
- ↳ SuperPDF
- ↳ 551einv
- ↳ E3進銷存
- 程式語言
- ↳ PHP
- ↳ CodeLobster PHP Edition
- ↳ Yii
- ↳ CodeIgniter
- ↳ Phalcon
- ↳ Symfony
- ↳ FuelPHP
- ↳ Zend Framework 2
- ↳ laravel
- ↳ WordPress
- ↳ ASP.NET/C#
- ↳ ASP/VBScript
- ↳ JSP
- ↳ Java Servlets
- ↳ ColdFusion
- ↳ Perl
- ↳ Java Script
- ↳ jQuery
- ↳ HTML + CSS
- ↳ jQuery
- ↳ nodejs
- ↳ VB6
- ↳ Git
- ↳ App Inventor 2
- ↳ bash
- ↳ C++/ VC/ OpenCV
- ↳ OpenCV
- ↳ go
- ↳ cordova
- ↳ python
- ↳ Xamarin
- ↳ Assembly
- 資料庫
- ↳ MySQL
- ↳ PostgreSQL
- ↳ ORACLE
- ↳ Access
- ↳ SQL Server
- ↳ SQLite
- ↳ MariaDB
- ↳ Mongodb
- 作業系統
- ↳ Linux
- ↳ Ubuntu
- ↳ CentOS
- ↳ Mint
- ↳ Mandriva
- ↳ Debian
- ↳ Red Hat Enterprise Linux
- ↳ Oracle Linux
- ↳ Fedora
- ↳ Kali Linux
- ↳ OpenSUSE
- ↳ Elementary OS
- ↳ Microsoft
- ↳ Server 2008 R2
- ↳ Server 2012 R2
- ↳ Server 2012
- ↳ 8
- ↳ 10
- ↳ System Center 2016
- ↳ NOVELL
- ↳ FreeBSD
- ↳ VMware
- ↳ VirtualBox
- ↳ Mac OS X
- ↳ Solaris
- ↳ iOS
- ↳ Android
- ↳ Cloud
- ↳ OpenStack
- ↳ Docker
- ↳ Proxmox VE
- ↳ CloudReady
- ↳ chrome
- 網頁伺服器
- ↳ apache
- ↳ tomcat
- ↳ nginx
- ↳ IIS
- ↳ JBoss
- ↳ weblogic
- ↳ WebHosting
- 硬體
- ↳ 硬體及週邊
- ↳ RouterOS
- ↳ LEGO NXT
- ↳ Arduino
- ↳ MSP430
- ↳ Raspberry Pi
- ↳ OpenERP
- ↳ Storage
- ↳ Server
- ↳ Brocade
- ↳ MODELS
- ↳ FortiGate
- 軟體
- ↳ sublime
- ↳ LibreNMS