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

christopher's avatar

PHP Fractal - must be of the type array

I am trying to destructure my API with https://fractal.thephpleague.com. So i am getting the data from an API as json back with Guzzle /json_decode($response->getBody(), true);. The object looks like this:

{
"count": 4,
"next": null,
"previous": null,
"results": [

Now i am trying to modify the response with Fractal:

$object = json_decode($response->getBody(), true);
$fractal = new Manager();
....
$resource = new Collection($object, function(array $books) {
            return [
                'results_size'      => $books['count'],
            ];
        });
$result = $fractal->createData($resource)->toArray();
return $result;

But i am getting only the error that i need an array.

Argument 1 passed to App\Http\Controllers\MyController::App\Http\Controllers\{closure}() must be of the type array, integer given

I also tried to pass (array) $response->getBody() or even $response->getBody() without success. Can someone help me and give me a little tip what i am doing wrong here?

0 likes
1 reply
bobbybouwmann's avatar

It doesn't seem to be a problem in your code itself, but more in the route. It says that your controller expects to get an argument of the type array, but you give it an integer. Can you show your controller action?

Please or to participate in this conversation.