Skip to content

Conversation

@tabennett
Copy link
Contributor

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)

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants