AllowedFields interaction with Laravel API Resources #615
Unanswered
vincent-lu
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi I'm trying to figure out what my problem is in getting
allowedFields
to play nice with Laravel API ResourcesJsonResource::whenLoaded
.The basic setup is something like this:
In my
App\Http\Controllers\ProductsController
:In my
App\Models\Product
:For my
App\Http\Resources\ProductResource
:App\Http\Resources\ProductStockResource
:When I send the request to
ProductsController::index()
with?include=stock&fields[products]=sku&fields[stock]=qty
I'm expacting to get something like:But stead I get:
It seems when any
fields[]
parameter is supplied, the$this->whenLoaded('stock')
inApp\Http\Resources\ProductResource
returnsnull
; but If I request?include=stock
only (i.e. without triggeringallowedFields
),$this->whenLoaded('stock')
returnsApp\Models\ProductStock
as expected.Let me know if anyone knows why
fields[]
parameter would make Laravel API Resources think the relationship isn't loaded?Beta Was this translation helpful? Give feedback.
All reactions