I've looked through the docs and the source code in great detail and am having trouble working out how I can supply the id field in the request and for that id to be used when creating the resource. For example, here's a sample request from my app:
"data": {
"type": "locums",
"id": "A5C5C2FD-29EC-4D6F-8342-BAD7D90AA5FD",
"attributes": {
"name": "Test Test",
"email": "[email protected]",
"password": "abcd1234",
"password_confirmation": "abcd1234"
},
"relationships": {
"region": {
"data": { "type": "regions", "id": "6F0073EC-3C98-4831-9595-890DF79E6E7D" }
},
"locum-type": {
"data": { "type": "locum-types", "id": "14C179EA-363F-4585-8879-6111CD162D62" }
}
}
}
}
And here's the response:
{
"data": {
"type": "locums",
"id": "2DAF7BD8-E6A1-41DB-85DB-FC5FC9ADCCAF",
"attributes": {
"created-at": "2017-08-08T09:49:57+01:00",
"updated-at": "2017-08-08T09:49:57+01:00",
"email": "[email protected]",
"name": "Test Test",
"image": null,
"contact-number": null,
"rcvs-number": null,
"search-location": null,
"search-latitude": null,
"search-longitude": null,
"search-distance": null,
"search-min-rate": null,
"search-max-rate": null,
"payment-details": null
},
"relationships": {
"locum-type": {
"data": {
"type": "locum-types",
"id": "14C179EA-363F-4585-8879-6111CD162D62"
},
"links": {
"self": "https://app.dev/api/locums/2DAF7BD8-E6A1-41DB-85DB-FC5FC9ADCCAF/relationships/locum-type"
}
},
"region": {
"data": {
"type": "regions",
"id": "6F0073EC-3C98-4831-9595-890DF79E6E7D"
},
"links": {
"self": "https://app.dev/api/locums/2DAF7BD8-E6A1-41DB-85DB-FC5FC9ADCCAF/relationships/region"
}
}
},
"links": {
"self": "https://app.dev/api/locums/2DAF7BD8-E6A1-41DB-85DB-FC5FC9ADCCAF"
}
}
}
I was expecting the two id fields to match. Any idea of what I'm doing wrong? I can supply any other code (schemas, hydrators etc.) if required.
Thanks in advance!
Hi. There's currently a test for client generated ids that are used for a non-Eloquent record:
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/Http/Controllers/SitesController.php#L40
They aren't however tested for Eloquent because I haven't needed to use them with Eloquent yet - but it's obviously something that needs to be supported.
Is the use case you have Eloquent? I can think of a way to do it but would be good if you can confirm what you're trying to use it with first.
Hi,
Yes, I'm using Eloquent models.
Glad to hear it's not me missing something! To be honest, I'm only wanting to use it for testing purposes, so I can create, view and update a resource in Postman. My client app won't be generating IDs. However, you're right in that as it's part of the JSON API specification, it's probably something that needs implementing.
Let me know if I can be of help in any way.
Cheers,
Dan
I think I know how to implement it in the current version, so when I've got a chance I'll add a test for it to see if it works. Will get back to you.
Hi. So I've quickly added a property to the Eloquent hydrator so that this is supported:
https://github.com/cloudcreativity/laravel-json-api/blob/develop/src/Hydrator/EloquentHydrator.php#L49-L60
You'll need to be on the dev branch to use it (will be good if you can try it to check it works as you'd expect):
$ composer require cloudcreativity/laravel-json-api:dev-develop
I can't release it as a tagged version just yet because I'll need to add validation of client generated ids first, which needs to be added to the base library as per this:
https://github.com/cloudcreativity/json-api/issues/6
I'll see if I can get a tagged release version for you once I've looked at what changes are required to the base package to get the client id validated. Not sure when this week I'll get to it but I'll update this issue when I have.
Please take care to note that the spec does not strictly require UUID format for client-generated IDs. They just have to be unique.
http://jsonapi.org/format/#crud-creating-client-ids
So I would suggest three hydrator state settings for client generation of IDs:
In my case, I am developing a new web-based data product for my company to replace a wide world of spreadsheets. While prototyping I create new "project" entries which mirror those from the official spreadsheet record, and during this time I am manually inputting the project ID number so that it matches the official record. As a result, I have cause to allow integer-based custom ID numbers in some forms for now. Later it will be automatic only so I want to stick with integers.
@SirLamer yes, you're spot on about that. the plan is to use a UUID regex as the default validation for client ids, but allowing a custom regex to be used.
Most helpful comment
I'll see if I can get a tagged release version for you once I've looked at what changes are required to the base package to get the client id validated. Not sure when this week I'll get to it but I'll update this issue when I have.