Skip to content

Fix many-to-many embedCount() when multiple relations are eager loaded#286

Merged
techmahedy merged 1 commit into
doppar:3.xfrom
techmahedy:techmahedy-3.x
Jun 4, 2026
Merged

Fix many-to-many embedCount() when multiple relations are eager loaded#286
techmahedy merged 1 commit into
doppar:3.xfrom
techmahedy:techmahedy-3.x

Conversation

@techmahedy
Copy link
Copy Markdown
Member

Summary

This PR fixes wrong relationship_count values when embed() is used with multiple relations.

Problem

When category, user, and tags were eager-loaded together, the many-to-many count logic used stale relation key state and could return incorrect counts.

Solution

The framework now uses the model primary key directly for many-to-many count grouping instead of relying on mutable relation state.

Changes

  • Fixed many-to-many count key resolution in Builder.php
  • Removed the stale $localKey dependency in count handling

Verification

  • Confirmed Post::embed(['category:name', 'user:name', 'tags:name'])->embedCount('tags') returns correct counts

@techmahedy techmahedy added the bug Something isn't working label Jun 4, 2026
@techmahedy techmahedy merged commit 9c05349 into doppar:3.x Jun 4, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant