Skip to content

Included morph to many data is null #280

Open
@maartenpaauw

Description

@maartenpaauw

The use case is, I've a Round that has many TextOverrides and belongs to many DefaultTexts. Within the RoundSchema I've added the following field:

MorphToMany::make('texts', [
    BelongsToMany::make('defaultTexts'),
    HasMany::make('textOverrides'),
])->readOnly(),

When I navigate to http://localhost/api/v1/rounds/1/texts, I see both default-texts and text-overrides nicely listed. But when I try to include texts, using http://localhost/api/v1/rounds/1?include=texts, I get the following exception message:

The attribute [texts] either does not exist or was not retrieved for model [App\Models\Round].

This exception message is displayed because we prevent accessing missing attributes using; Model::preventAccessingMissingAttributes();. When I remove this line the view round API endpoint loads, but has the following outcome. The data is null which is incorrect because there are default-texts and text-overrides.

{
    "jsonapi": {
        "version": "1.0"
    },
    "links": {
        "self": "http://localhost/api/v1/rounds/1"
    },
    "data": {
        "type": "rounds",
        "id": "1",
        "attributes": {
            "name": "Round 1"
        },
        "relationships": {
            "texts": {
                "links": {
                    "related": "http://localhost/api/v1/rounds/1/texts",
                    "self": "http://localhost/api/v1/rounds/1/relationships/texts"
                },
                "data": null
            }
        },
        "links": {
            "self": "http://localhost/api/v1/rounds/1"
        },
        "meta": {
            "createdAt": "2024-05-15T07:48:27.000000Z",
            "updatedAt": "2024-05-15T07:48:34.000000Z"
        }
    }
}

I do have added a TextCollectionQuery, which extends the ResourceQuery, and registered in the serving method of the Server class, as described on the documentation page; https://laraveljsonapi.io/docs/3.0/digging-deeper/polymorphic-to-many.html#query-parameters.

At this point I've no idea what's wrong with the implementation. What am I missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions