Lomkit / laravel-rest-api

Generate Api in seconds
https://laravel-rest-api.lomkit.com/
MIT License
297 stars 18 forks source link

How to sync multiple relations? #95

Open Stiropor opened 6 months ago

Stiropor commented 6 months ago

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?

GautierDele commented 6 months ago

Hello @Stiropor,

I understand your problem, this might be a good option to be able to provide multiple keys as you are giving me the point, I'll put this as a new feature 😄

While you wait for this to be done, what you can simply do is remove the "without_detaching" from the first argument, this will detach all former ids do you see what I mean ?

Stiropor commented 5 months ago

While you wait for this to be done, what you can simply do is remove the "without_detaching" from the first argument, this will detach all former ids do you see what I mean ?

This works, yes. The problem with this is when all tags are deleted, I cannot send empty [] and ask for sync. I'd always need to know which tags there were before.

GautierDele commented 5 months ago

I'm not sure I understand your problem, could you please precise it ?

Stiropor commented 5 months ago

I'm not sure I understand your problem, could you please precise it ?

Certainly. I'm using this to handle belongsToMany relation, in this case article with tags. When adding tags, there is no problem. When modifying them I can also use sync (without without_detaching on first one it works). But when editing an article and all tags are removed there is nothing to send to use sync as I'd normally use it in Laravel to sync relations. The only way would be to somehow remember original attributes of article, which is kinda unusual and not useful otherwise. Doing a sync with [] would be great in this case or sending empty key, which of course is not the current feature. Are there any other way of removing all relations (tags for example) from a spcific entity (article for example)?