Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to-many relation bug #53

Closed
heyiammushu opened this issue Mar 19, 2021 · 1 comment
Closed

Update to-many relation bug #53

heyiammushu opened this issue Mar 19, 2021 · 1 comment

Comments

@heyiammushu
Copy link

Trying to update relations of workers resource

PATCH /api/v1/workers/1
...
'type' => 'workers',
'id' => "1",
'attributes' => [...],
'relationships' => [
    'skills' => [
        'data' => [
            'type' => 'skills',
            'id' => '4',
        ]
    ]
]
...

And getting the response

"errors": [
      {
          "detail": "The field skills must be a to-many relation.",
          "source": {
              "pointer": "/data/relationships/skills"
          },
          "status": "400",
          "title": "Non-Compliant JSON:API Document"
      }
  ],

Workers schema

return [
     ...
      BelongsToMany::make('skills'),
      ...
  ];

Workers resource

public function relationships($request): iterable
    {
        return [
            ...
            $this->relation('skills'),
            ...
        ];
    }

Workers request

public function rules(): array
    {
        return [
            'skills' => JsonApiRule::toMany(),
            ...
        ];
    }

Workers model

...
public function skills()
    {
        return $this->belongsToMany(Skill::class)->withTimestamps();
    }
...
@lindyhopchris
Copy link
Contributor

The error message is correct. Your payload should be this if the relationship is to-many:

PATCH /api/v1/workers/1
...
'type' => 'workers',
'id' => "1",
'attributes' => [...],
'relationships' => [
    'skills' => [
        'data' => [
            [
                'type' => 'skills',
                'id' => '4',
            ]
        ]
    ]
]
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants