Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Compoships Laravel Package

awobaz/compoships

Adds composite-key relationship support to Laravel Eloquent. Define hasOne/hasMany/belongsTo relations matching two or more columns so eager loading works with legacy or third‑party schemas, using a custom base Model or Compoships trait.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Strengths:

    • Composite Key Support: Directly addresses Laravel’s lack of native support for multi-column relationships, enabling complex data models (e.g., multi-tenant, hierarchical, or multi-dimensional schemas) without schema refactoring.
    • Backward Compatibility: Works alongside existing Eloquent relationships, reducing risk of breaking changes in legacy systems.
    • Standardized Syntax: Mimics Laravel’s native relationship syntax, lowering the learning curve for developers.
    • Performance: Optimized for eager loading and bulk operations (e.g., attach(), sync()), critical for scalable applications.
    • Many-to-Many Support: Extends Laravel’s belongsToMany to composite keys, a gap in core functionality.
  • Limitations:

    • No Native Composite Primary Keys: Requires single-column primary keys in tables (per Laravel’s recommendation), limiting use cases where tables inherently use composite keys (e.g., junction tables).
    • Nullable Column Quirks: Relationships with all-null composite keys are unsupported, which may require workaround logic for sparse data.
    • Trait/Inheritance Dependency: Models must either extend Awobaz\Compoships\Database\Eloquent\Model or use the Compoships trait, which could conflict with existing inheritance hierarchies or third-party traits.

Integration Feasibility

  • High for Laravel Projects:
    • Minimal Boilerplate: Installation via Composer and trait/application is straightforward.
    • Database Agnostic: Works with any database supported by Laravel (MySQL, PostgreSQL, SQLite, etc.).
    • Testing Support: Comprehensive unit tests and CI matrix ensure compatibility across Laravel/PHP versions (tested up to Laravel 13).
  • Challenges:
    • Migration Path: Existing relationships using where clauses (e.g., hasMany(Task::class)->where('team_id', $this->team_id)) must be refactored to use Compoships syntax.
    • Custom Pivot Models: For belongsToMany, custom pivot models must extend Awobaz\Compoships\Database\Eloquent\Relations\Pivot, requiring updates to existing pivot classes.
    • Factory Integration: Factories must use Awobaz\Compoships\Database\Eloquent\Factories\ComposhipsFactory for relationship testing, adding a dependency.

Technical Risk

  • Low to Moderate:
    • Proven Stability: 1,200+ stars, active maintenance (last release: 2026-04-29), and contributions from the Laravel community reduce risk.
    • Documentation: README and examples cover 90% of use cases, though edge cases (e.g., nullable columns) may need experimentation.
    • Dependency Risks: MIT license and no external dependencies (beyond Laravel) minimize legal/integration risks.
  • Mitigation Strategies:
    • Pilot Testing: Start with non-critical relationships in a staging environment.
    • Rollback Plan: Use feature flags or database views to revert to where-based queries if needed.
    • Performance Benchmarking: Test eager loading and bulk operations under production-like loads.

Key Questions for Stakeholders

  1. Architecture:
    • Are there existing models with composite-key relationships that must be supported without schema changes?
    • How critical is support for belongsToMany with composite keys (e.g., for multi-tenant pivot tables)?
  2. Development:
    • Can teams adopt the Compoships trait/inheritance without disrupting existing model hierarchies?
    • Are there custom pivot models or factories that would require updates?
  3. Performance:
    • Will the application rely heavily on eager loading or bulk operations (e.g., attach())? If so, has Compoships been benchmarked for these workloads?
  4. Long-Term:
    • Does the team plan to migrate toward single-column primary keys eventually, or is this a permanent solution?
    • Are there plans to extend this to other ORMs (e.g., Doctrine) or frameworks?

Integration Approach

Stack Fit

  • Primary Use Cases:
    • Legacy Systems: Integrate with databases using composite keys (e.g., ERP, CRM, or legacy Laravel apps) without schema refactoring.
    • Multi-Dimensional Relationships: Model complex hierarchies (e.g., user-team-project-category assignments) or multi-tenant data.
    • Third-Party APIs: Connect to external systems with composite-key schemas (e.g., payment gateways, inventory systems).
  • Compatibility:
    • Laravel Versions: Tested up to Laravel 13; likely compatible with Laravel 14+ (check for breaking changes).
    • PHP Versions: Supports PHP 8.1+ (aligns with Laravel’s current LTS).
    • Database Drivers: Works with all Laravel-supported databases (MySQL, PostgreSQL, SQLite, SQL Server).
    • Caching: Compatible with Laravel’s cache drivers (e.g., Redis, Memcached) for relationship results.

Migration Path

  1. Assessment Phase:
    • Audit existing relationships using where clauses or manual joins for composite keys.
    • Identify models requiring Compoships (e.g., those with multi-column foreign keys).
  2. Pilot Implementation:
    • Start with non-critical models (e.g., reporting tables, auxiliary relationships).
    • Replace where-based relationships with Compoships syntax:
      // Before
      return $this->hasMany(Task::class)->where('team_id', $this->team_id);
      
      // After
      return $this->hasMany(Task::class, ['team_id', 'category_id'], ['team_id', 'category_id']);
      
  3. Incremental Rollout:
    • Update belongsToMany relationships with composite keys first (highest impact).
    • Refactor factories to use ComposhipsFactory trait.
    • Update custom pivot models to extend Awobaz\Compoships\Database\Eloquent\Relations\Pivot.
  4. Testing:
    • Validate eager loading (with()), bulk operations (attach(), sync()), and existence queries (has(), whereHas()).
    • Test edge cases (e.g., nullable columns, empty relationships).

Compatibility Considerations

  • Existing Code:
    • Query Builders: Compoships integrates with Laravel’s query builder, so raw queries using DB::table() remain unaffected.
    • Accessors/Mutators: No changes required; Compoships operates at the relationship level.
    • Scopes: Custom scopes using where clauses may need updates if they rely on model attributes not yet loaded (e.g., during eager loading).
  • Third-Party Packages:
    • Packages extending Eloquent (e.g., Spatie’s media library, Laravel Nova) may conflict if they also use traits/inheritance. Test thoroughly.
    • ORM-agnostic packages (e.g., Laravel Scout) are unaffected.

Sequencing

  1. Phase 1: Core Relationships
    • Replace hasMany, belongsTo, and belongsToMany with composite keys.
    • Focus on read-heavy operations first (e.g., API responses, reporting).
  2. Phase 2: Write Operations
    • Implement attach(), sync(), and detach() for belongsToMany relationships.
    • Validate data integrity (e.g., ensure composite keys are unique in pivot tables).
  3. Phase 3: Edge Cases
    • Handle nullable columns (may require application-level logic).
    • Optimize performance for large datasets (e.g., indexing composite keys in the database).
  4. Phase 4: Testing and Documentation
    • Add integration tests for new relationships.
    • Document Compoships-specific patterns (e.g., input shapes for attach()).

Operational Impact

Maintenance

  • Pros:
    • Reduced Technical Debt: Eliminates workarounds (e.g., where clauses in relationships) and manual joins.
    • Consistent Behavior: Standardizes composite-key relationships across the codebase.
    • Community Support: Active maintenance and contributions from the Laravel ecosystem.
  • Cons:
    • Dependency Management: Requires monitoring for Compoships updates (e.g., Laravel 14 compatibility).
    • Debugging Complexity: Composite-key relationships may introduce harder-to-diagnose issues (e.g., silent failures with all-null keys).
  • Tools:
    • Laravel Debugbar: Add Compoships-specific tabs to visualize composite relationships.
    • Logging: Instrument relationship queries to log composite key values for debugging.

Support

  • Developer Onboarding:
    • Training: Document Compoships syntax and edge cases (e.g., attach() input shapes) in the team’s style guide.
    • Code Reviews: Enforce checks for:
      • Consistent trait/inheritance usage across related models.
      • Proper handling of nullable columns.
      • Input validation for attach()/sync() operations.
  • Common Issues:
    • Eager Loading Failures: Ensure all composite keys are loaded before eager loading (e.g., use with() on parent models).
    • Input Shape Errors: Validate attach()/sync() inputs to avoid InvalidUsageException.
    • Performance Bottlenecks: Composite-key joins may require database indexing.

Scaling

  • Performance:
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui