laravel-json-api / laravel

JSON:API for Laravel applications
MIT License
541 stars 41 forks source link

Missing relationships for included resource #254

Open bbprojectnet opened 1 year ago

bbprojectnet commented 1 year ago

Hi,

I have weird case, i have simple polls with hasMany questions resources. Questions resource also have self reference for parent question (enabledIfQuestion).

When i try to GET /api/polls/{id}?include=questions,questions.enabledIfQuestion everything works fine, but, when i define questions relationship in Poll model with ->orderBy(), like that:

    public function questions(): HasMany
    {
        return $this->hasMany(PollQuestion::class)->orderBy('order');
    }

then, in response, some (but not all) records has missing relationships in included section.

Without order:

        {
            "type": "poll-questions",
            "id": "gAa0pkGKYEZJYRxlXenVo5d4",
            "attributes": {
                ...
            },
            "relationships": {
                "enabledIfQuestion": {
                    "data": {
                        "type": "poll-questions",
                        "id": "Q0OkPGeARaLdERrdmwn1op9Y"
                    }
                }
            }
        },

With order:

        {
            "type": "poll-questions",
            "id": "gAa0pkGKYEZJYRxlXenVo5d4",
            "attributes": {
                ...
            }
        },