#60126·framework

JsonApiResource always returning eager-loaded relationships on nested relations

Author: simon-tmaCreated May 14, 2026Updated Aug 15, 2026

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

php
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.