cloudcreativity / laravel-json-api

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

How to get meta in resource identifier object? #468

Closed stefanwub closed 3 years ago

stefanwub commented 4 years ago

Is there a way to include a meta member to the resource identifier object?

Expected outcome:

{
    "data": {
        "type": "products",
        "id": "7",
        "attributes": {
            "sku": "Sku"
        },
        "relationships": {
            "warehouses": {
                "data": [
                    {
                        "type": "warehouses",
                        "id": "1",
                        "meta": {
                             "stock": 10
                         }
                    },
                    {
                        "type": "warehouses",
                        "id": "2",
                        "meta": {
                             "stock": 5
                         }
                    }
                ],
            }
            ...
        }
    },
    ...
}

I manage to add meta to the relationship object according to the code from the documentation. See below. But I couldn't find a way to add this meta to each data member in the relationship array.

public function getRelationships($post, $isPrimary, array $includeRelationships)
{
    return [
        'comments' => [
            self::META => function () use ($post) {
                return [
                    'total' => $post->comments_count,
                ];
            },
        ],
    ];
}

The JSON API specifications says: A “resource identifier object” MAY also include a meta member, whose value is a meta object that contains non-standard meta-information. (https://jsonapi.org/format/#document-resource-identifier-objects).

Would be great if this could be implemented!

lindyhopchris commented 4 years ago

Hmmm... unfortunately this isn't supported by the version of the encoder we're on. I'm not even sure if it's supported by the latest version of the encoder that we plan to upgrade to.

For the meantime you'll have to use meta at the relationship level. I'll leave this issue open and if it's not supported when we upgrade the encoder, I'll have to open an issue in that repo to get it added (or do a custom extension of the encoder).

lindyhopchris commented 3 years ago

Closing this as it is supported in the new laravel-json-api/laravel package: https://laraveljsonapi.io/docs/1.0/resources/meta.html#identifier-meta