Closed genyded closed 3 years ago
So I'm not massively inclined to support conditional attributes. The reason is because it is really inefficient to have to loop over all the attributes to check if they are conditional or not, and filter them out. Doing this when you're serializing lots of resources for a single response is going to take a big hit.
So for the current version of the package, you will need to use the `if statements - though I do agree they are not clean!
For v2, I'll investigate whether I can support conditional attributes - but will only do so if they do not have a performance impact. I can think of potential ways of dealing with them though, but it depends on hooking into the internals of the encoder - which is exceptionally difficult to do because it is provided by another package that has an aversion to such internal modifications!
You might want to take a look at #411 as what you're asking is kind of related to that!
Cool - this is of paramount importance to us, but performance is a key consideration as well. Laravel seems to have provided both (albeit not in JSON API format). We'll leave it to you to decide if you want to leave this open for specific tracking or close it in favor of consideration under #411 .
Thanks, will leave open on the 2.0
milestone so I don't forget about it!
Conditional atttributes are fully implemented in the new package laravel-json-api/laravel
- see https://laraveljsonapi.io
Could not find this in the docs anywhere, but is there any way to do conditional attributes along the lines of Laravel's resource attributes $this->when?
We know that we can do something like:
However that also removes any other attributes with empty values which may not be desired.
We could also do something like:
... which preserves other attributes with empty values, but is not very clean , especially if you have many conditional attributes. Each would need it's own IF(s). Was hoping for a clean solution or plans to implement one. In real world apps, it's not likely that all users should see all attributes for all api resources at all times.