承接 mathieutu/laravel-json-syncer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mathieutu/laravel-json-syncer

Composer 安装命令:

composer require mathieutu/laravel-json-syncer

包简介

Export and import your Eloquent models to/from JSON

README 文档

README

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

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;
    // ...
}

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 the $jsonExportableAttributes and $jsonExportableRelations properties or overwriting the 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 the $jsonImportableAttributes and $jsonImportableRelations properties or overwriting the getJsonImportableAttributes() and getJsonImportableRelations() 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 :

{
    "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 another 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.

mathieutu/laravel-json-syncer 适用场景与选型建议

mathieutu/laravel-json-syncer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.11k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2017 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mathieutu/laravel-json-syncer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mathieutu/laravel-json-syncer 我们能提供哪些服务?
定制开发 / 二次开发

基于 mathieutu/laravel-json-syncer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 33.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 30
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 29
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-07