cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
778 stars 109 forks source link

Error when included resources #604

Closed lironesamoun closed 3 years ago

lironesamoun commented 3 years ago

hi,

I try to do the following:

http://127.0.0.1:8000/api/v1/assets/27?include=histories

The relationship is defined between an asset and a history. An asset has multiple histories and one history belong to an asset.

I have this error:

ErrorException
assert(): assert(is_array($data) === true || $data instanceof Closure || is_object($data) === true || $data === null) failed 

On my Schema.php

public function getRelationships($asset, $isPrimary, array $includeRelationships)
    {

        return [
            'histories' => [
                self::SHOW_SELF => true,
                self::SHOW_RELATED => true,
                self::SHOW_DATA => isset($includeRelationships['histories']),
                self::DATA => function () use ($asset) {
                    return "$asset->name";
                },
            ],
        ];
    }

On the validator, the "histories" is included to the array.

Could you please help on that ? Thank

lindyhopchris commented 3 years ago

The answer is this line is incorrect:

return "$asset->name";

You're returning a string, which is not a valid value for relationship data.

Should be:

return $asset->histories

assuming the relationship is called histories on your Asset model class.