sarala
sarala copied to clipboard
how to POST with relationship
https://jsonapi.org/format/#crud-creating
how do i send post request with relationships
POST /photos HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "photos",
"attributes": {
"title": "Ember Hamster",
"src": "http://example.com/images/productivity.png"
},
"relationships": {
"photographer": {
"data": { "type": "people", "id": "9" }
}
}
}
}
@milroyfraser I would like to know about this as well. Simply assigning a loaded model to a relation property does not seem to work.
@asadhshujau @happyDemon You can't. JSON API SPEC does't support that at the moment.
You should send separate requests to create related resources (ex. POST /people
)
And only then, if needed, associate newly created related resources with your main resource. Again, with different request (sync/attach).
@subotkevic I believe that @asadhshujau and @happyDemon are attempting to link existent resources in the request, not create new ones. In which case, that is inaccurate. See #30.
That being said, it’s possible and works fine.
a few years later still does not work :D or i am not getting it to work.
can you provide an example @subotkevic ?
my use case:
User model
name, email, hasOne Brand
Brand
name
I need to create a user with a Brand right away, but the library sends the brand
key inside user
attributes and not in the relationships
key.