Skip to content

JsonApiResource always returning eager-loaded relationships on nested relations #60126

Description

@simon-tma

Laravel Version

12.58.0 and 13.9.0

PHP Version

8.5.6

Database Driver & Version

No response

Description

JsonApiResource has the includePreviouslyLoadedRelationships() method to always include relationships that have been eager loaded, as well as those listed in include, with the default being to not to.

However, any nested relations that have been eager loaded are always being included if the intermediate relation is included.

Steps To Reproduce

class Post extends Model {
  public function comments(): HasMany { return $this->hasMany(Comment::class); }
}

class Comment extends Model {
  public function author(): BelongsTo { return $this->belongsTo(User::class); }
}

class CommentResource extends JsonApiResource {
  public $relationships = ['author'];
}

class PostResource extends JsonApiResource {
  public $relationships = ['comments'];
}

Router::get('/api/post/{post}', fn (Post $post) => new PostResource($post->load('comments.author));

curl localhost/api/post/1?include=comments

The return includes the author objects, even though not requested.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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