Repository navigation
Conversation
tabennett
force-pushed
the
feature/add-with-count
branch
from
May 6, 2026 17:34
4a4b983 to
2e545e5
Compare
Implements withCount() method to count related records without loading them, providing significant performance improvements when only counts are needed. Changes: - Add withCount() method to Builder with support for constraints and aliasing - Implement toCountQuery() interface method across all relationship types - Add getBaseQueryWithScopes() helper to BaseRelationship for scope application - Update Custom relationship to accept optional count closure parameter - Add 23 comprehensive integration tests covering all relationship types - Document withCount() in eager-loading.md, query-building.md, and relationship-cheatsheet.md Features: - Count single or multiple relationships in one query - Apply constraints to count queries via closures - Custom aliases using 'as' syntax - Automatic snake_case conversion for count attributes - Global scopes (SoftDeletes, etc.) automatically applied - Support for HasMany, HasOne, BelongsTo, BelongsToMany relationships - Custom relationships can opt-in via count closure Technical Details: - Delegates count logic to relationships via polymorphic toCountQuery() method - Wraps constraint closures in Holloway Builder for full query API access - Preserves relationship-level scopes during counting - Count columns added as subqueries on main query Tests: All 23 tests passing Breaking Changes: None (Custom relationships without count closure throw helpful exception)
tabennett
force-pushed
the
feature/add-with-count
branch
from
October 3, 2026 21:44
2e545e5 to
4065545
Compare
…eys, and or constraints - Correlate has-one/has-many/belongs-to-many counts on the relationship's own local key, as load() does, instead of the mapper's primary key. - Alias the related table when it is also the parent table, as Eloquent does for self relations, so a self-referential count no longer correlates the table with itself. Add Builder::aliasTable() for this. - Build count queries on the related mapper's query builder: relationships apply the constraints and then the global scopes once, last, so constraints can use withoutGlobalScope() and scopes are no longer applied twice. This adds an optional constraints argument to Relationship::toCountQuery(). - Apply constraints as a scope (Builder::callScope() is now public, as in Eloquent) so an or in a constraint stays inside the correlation instead of matching rows of other parents. - Accept an uppercase AS when aliasing a count, and point the custom relationship's missing count closure error at Mapper::custom(). - Add a self-referential category fixture and tests for local keys, self-referential counts, or constraints, and custom relationship counts.
- Explain that the count is a column on the record passed to hydrate() and must be left out of dehydrate(). - Document constraint behavior (unqualified columns, scopes applied after, withoutGlobalScope) and same-table relationships. - Fix the cheatsheet example, which used typographic quotes in PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements withCount() method to count related records without loading them, providing significant performance improvements when only counts are needed.
Changes:
Features:
Technical Details:
Tests: All 23 tests passing
Breaking Changes: None (Custom relationships without count closure throw helpful exception)