Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

connecteev's avatar

Why does this fail? (Retrieving return data from an API response)

Hopefully a simple question...but this one has been driving me a bit nuts. I am trying to retrieve the return data from an API response. In this example, hardcoded to:

$returnData=[];
// $returnData['posts'] = null;
$result = (new PostResource($returnData))->response()->setStatusCode(Response::HTTP_OK);
return $result; // this is what the API returns

If the caller tries to access the API return data like this:

$result->getData(); // This fails.

Error:

UnexpectedValueException: The Response content must be a string or object implementing __toString(), "object" given. 

Why??

0 likes
1 reply
connecteev's avatar
connecteev
OP
Best Answer
Level 11

Here's the solution:

$result->getData(); // This fails.
$result->getData()->data; // this works

No idea why, but it works.

1 like

Please or to participate in this conversation.