Reputation: 945
I'm creating an API using Laravel with the laravel json-api package.
I'm trying to create an endpoint that will return a set of calculated values for a user. The endpoint will be returning a set of products with information about whether a user has access to them, and if they do, when their access expires.
As an example I want to be able to make a call to /api/v1/users/1234/access
and receive a response that is json api response that looks something like this:
{
"jsonapi": {
"version": "1.0"
},
"data": {
"type": "user-access",
"id": "?????",
"attributes": {
"product1": {
"active": true,
"expires": "2024-10-01T23:59:59Z"
},
"product2": {
"active": false,
},
}
},
"links": {
"self": "https://site.test/api/v1/users/1234/access"
}
}
The list of products can change based on what the user purchases, and the access is derived from various database queries and calculations.
My issue is that I'm unsure how best to implement this within the confines of how Laravel JSON:API works. I know it has an available package to do non-eloquent based models, but I'm not sure if this is what I need. The access information isn't really a resource in the strictest sense. There can't be a /api/v1/user-access
endpoint because it can't exist without the context of a user. I suppose I could break out of the JSON:API package entirely and just use regular Laravel and manually create the required json api structure but I'd rather not do that if I don't have to.
In addition to the object itself, I need to be able to add a hasOne
relationship to the /users/1234
resource, but I'm having trouble figuring out a way to add a relationship to something that isn't a defined Schema.
I'd really appreciate an ideas you all have.
Upvotes: 1
Views: 54