Skip to content

How to sync multiple relations? #95

@Stiropor

Description

@Stiropor

Description

First of all - great package! Works really nice.

I do have a question - mutation endpoint supports also sync, but somehow I cannot figure out how to make sync for multiple related resources. For example an article has a BelongsToMany relation with tags. When tags change on an article - how do I send only new ids (keys) of tags to sync, as I would do with ->sync() method in Laravel?

This is what I'm using for now:

{
  "mutate": [
    {
        "operation": "update",
        "key": 42,
        "attributes": {
            "title": "Test"
        },
        "relations": {
            "tags": [
                {
                    "operation": "sync",
                    "without_detaching": true,
                    "key": 2
                },
                {
                    "operation": "sync",
                    "without_detaching": true,
                    "key": 3
                }
            ]
        }
    }
  ]
}

If I don't send without_detaching only last tag is then attached to articles. If I add without_detaching then new ones are added but old ones (id: 1) are still there.

It'd be maybe cool to either use keys (array) attribute, to sync multiple related models or just making key attribute also being allowed as an array, but this could of course make things more complex I suppose.

Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions