PHPackages                             prewk/seriquent - 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. prewk/seriquent

ActiveLibrary

prewk/seriquent
===============

Eloquent anonymized (de)serialization

0.6.8(9y ago)14.6k1[3 issues](https://github.com/prewk/seriquent/issues)MITPHPPHP &gt;=5.6

Since Sep 27Pushed 9y ago3 watchersCompare

[ Source](https://github.com/prewk/seriquent)[ Packagist](https://packagist.org/packages/prewk/seriquent)[ Docs](http://github.com/prewk/seriquent)[ RSS](/packages/prewk-seriquent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (26)Used By (0)

Seriquent [![Build Status](https://camo.githubusercontent.com/9c346882aad495baa6b06f9558403a88208126343c80124bdff1ea219b5c5f23/68747470733a2f2f7472617669732d63692e6f72672f707265776b2f736572697175656e742e737667)](https://travis-ci.org/prewk/seriquent) [![Coverage Status](https://camo.githubusercontent.com/4894915f7b70f593615133c089e4446216aa4ced017130e03e1f52092e4069b1/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f707265776b2f736572697175656e742f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/prewk/seriquent?branch=master)
==================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#seriquent--)

1. **Serialize** a tree of Eloquent models into a self-referential array with anonymized primary keys
2. **Deserialize** it back into the database with new primary keys

Installation
============

[](#installation)

`composer require prewk/seriquent`

Example
=======

[](#example)

Your database has a `foos` table, belonging to a `qux` table.

`foos` has a one-to-many relation to the `bars` table.

Both `bars` and `qux` have a polymorphic one-to-many relation to the `bazs` table (They are "`bazable`").

Database contents
-----------------

[](#database-contents)

```
                ╔═══════╗    ╔═══════╗
             +--║ Foo 5 ║----║ Qux 2 ║
             |  ╚═══════╝    ╚═══════╝
             |                   |
             |                   |
    /--------+--------\          |
╔═══════╗╔═══════╗╔═══════╗      |
║ Bar 2 ║║ Bar 3 ║║ Bar 4 ║      |
╚═══════╝╚═══════╝╚═══════╝      |
    |        |                   |
    |        |                   |
╔═══════╗╔═══════╗           ╔═══════╗
║ Baz 5 ║║ Baz 6 ║           ║ Baz 7 ║
╚═══════╝╚═══════╝           ╚═══════╝

╔═════════════════════╗
║ foos                ║
╟─────────────────────╢
║ id: 5               ║
║ qux_id: 2           ║
║ name: "Lorem ipsum" ║
╚═════════════════════╝
╔═══════════╗╔═══════════╗╔═══════════╗
║ bars      ║║ bars      ║║ bars      ║
╟───────────╢╟───────────╢╟───────────╢
║ id: 2     ║║ id: 3     ║║ id: 4     ║
║ foo_id: 5 ║║ foo_id: 5 ║║ foo_id: 5 ║
╚═══════════╝╚═══════════╝╚═══════════╝
╔═══════════════════════╗
║ quxes                 ║
╟───────────────────────╢
║ id: 2                 ║
║ data: ["a", "b", "c"] ║
╚═══════════════════════╝
╔═════════════════╗╔═════════════════╗╔═════════════════╗
║ bazs            ║║ bazs            ║║ bazs            ║
╟─────────────────╢╟─────────────────╢╟─────────────────╢
║ id: 5           ║║ id: 6           ║║ id: 7           ║
║ bazable_type: 5 ║║ bazable_type: 5 ║║ bazable_type: 5 ║
║ bazable_id: 5   ║║ bazable_id: 5   ║║ bazable_id: 5   ║
╚═════════════════╝╚═════════════════╝╚═════════════════╝

```

Desired results
---------------

[](#desired-results)

```
{
    "Foo": [
        { "@id": "@1", "qux": "@7", "name": "Lorem ipsum" }
    ],
    "Bar": [
        { "@id": "@2", "foo": "@1" },
        { "@id": "@4", "foo": "@1" },
        { "@id": "@6", "foo": "@1" }
    ],
    "Baz": [
        { "@id": "@3", "bazable": ["Bar", "@2"] },
        { "@id": "@5", "bazable": ["Bar", "@4"] },
        { "@id": "@8", "bazable": ["Qux", "@7"] }
    ],
    "Qux": [
        { "@id": "@7", "data": ["a", "b", "c"] }
    ]
}
```

- `Foo`, `Bar` etc are the Eloquent models' FQCNs
- All entities get a unique internal id `"@id": "@123"`
- An entity refers to another entity by its internal id and the relation name `"foo": "@1"`
- Regular columns are just values `"name": "Lorem ipsum"`

Eloquent models and serialization blueprints
--------------------------------------------

[](#eloquent-models-and-serialization-blueprints)

```
