laravel
laravel copied to clipboard
Missing relationships for included resource
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": {
...
}
},