As the title says, really. For example:
GET /employees/
{
"data": {
"type": "employees",
"id": "<id>",
"attributes": {
"first-name": "Scott",
"last-name": "Jones",
"full-name": "Scott Jones",
},
"relationships": {
"permissions": {
"data": []
}
},
"links": {
"self": "https://api.dev/employees/<id>"
}
}
}
PATCH /employees/
_Request:_
{
"data": {
"type": "employees",
"id": "<id>",
"attributes": {
"first-name": "Jim",
"last-name": "Brown"
},
"relationships": {
"permissions": {
"data": [
{ "type": "permissions", "id": "practicegroup-employees" },
{ "type": "permissions", "id": "practicegroup-practices" }
]
}
}
}
}
_Response:_
{
"data": {
"type": "employees",
"id": "<id>",
"attributes": {
"first-name": "Jim",
"last-name": "Brown",
"full-name": "Jim Brown",
},
"relationships": {
"permissions": {
"data": []
}
},
"links": {
"self": "https://api.dev/employees/<id>"
}
}
}
GET /employees/
{
"data": {
"type": "employees",
"id": "<id>",
"attributes": {
"first-name": "Jim",
"last-name": "Brown",
"full-name": "Jim Brown",
},
"relationships": {
"permissions": {
"data": [
{ "type": "permissions", "id": "practicegroup-employees" },
{ "type": "permissions", "id": "practicegroup-practices" }
]
}
},
"links": {
"self": "https://api.dev/employees/<id>"
}
}
}
Why do the attributes come through updated, but the relationships do not?
This looks like an Eloquent caching issue. Are you doing $model->permissions at any point during the request cycle? That will cause them to be cached against the model as an empty relationship, so when they are encoded an empty relationship is being given back instead of the changed relationship.
If that is the case, ideally the hydrator needs to force the relationship cache to be cleared for the specific relationship it has just modified. But I'm not sure how to do that for a specific relationship off the top of my head, so we might have to do a full refresh of the model.
Can you let me know if that is the problem?
I'm not calling it for the model being updated as far as I can see. I am getting the permissions of the current user, however.
Hmmm... have looked into this. I can only reproduce if I force the model to load the relationship prior to the update occurring. The hydrator doesn't cause it to load the relationship as it always uses the relation method.
Saying that, it probably shouldn't be assuming that the relationship isn't already loaded, as it would be reasonable for it to have been used in some other context, e.g. an authorizer or validator.
So I think the way to solve this for the current release is for the Eloquent controller to refresh the model after committing changes to it.
@danherd
can you try the fix branch that I've pushed and let me know if that fixes the issue for you?
$ composer require "cloudcreativity/laravel-json-api:dev-hotfix/0.11.3"
Yeah this fixes it. Having looked a bit closer, I was actually getting the permissions of the employee being updated in the authoriser class (to check the current user is allowed to modify them).
Great, glad that fixes it.
I'll need to test that hotfix branch in the two production applications I have to check that it's definitely ok before I tag it, but I don't envisage any problems. It'd be good to make the refresh of the model more intelligent - i.e. it only refreshes if it's modified any of the relationships, but will have to do that in a future release.
Released v0.11.3