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
Laravel Composite Relations

Laravel Composite Relations Laravel Package

reedware/laravel-composite-relations

View on GitHub
Deep Wiki
Context7

Adds the ability to have multiple foreign keys in a relation.

Frequently asked questions about Laravel Composite Relations
How do I define a composite BelongsTo relation in Laravel using this package?
Use the `belongsToComposite` method on your model, specifying the composite foreign keys and the related model’s primary key. For example: `belongsToComposite(['user_id', 'role_id'], 'id', UserRole::class)`. The package handles the SQL generation under the hood, so eager loading and `whereHas` work as expected.
Does this package support Laravel 12 or 13?
Yes, the latest version (6.x) explicitly supports Laravel 12.x and 13.x, with PHP 8.2+. Check the [versioning table](https://github.com/tylernathanreed/laravel-composite-relations#versioning) for compatibility with older Laravel versions. Always verify your Laravel version matches the package’s supported range.
Can I use composite relations with eager loading?
Absolutely. The package fully supports eager loading via `with()`. For example: `$users->with('compositeRole')` will load the composite relation efficiently. Under the hood, it generates optimized SQL with `WHERE (col1 = ? AND col2 = ?)` clauses, avoiding N+1 issues.
What if my related model also uses composite keys?
This package assumes the *related model’s primary key is a single column* (e.g., `id`). If your related model also uses composite keys, you’ll need to manually override the query logic or restructure your schema. The package doesn’t support composite-to-composite relations natively.
Are there performance concerns with composite relations in production?
Composite relations may introduce slightly slower queries due to additional `WHERE` clauses, but the package optimizes SQL generation. Test with `toSql()` or Laravel Debugbar to profile performance. For high-traffic apps, consider denormalization or surrogate keys if composite relations become a bottleneck.
How do I handle composite relations in migrations?
Add composite foreign keys to your migration using Laravel’s `foreign` constraints with multiple columns. For example: `$table->foreign(['user_id', 'role_id'])->references(['id', 'role_id'])->on('user_roles')`. Ensure existing data is backfilled if migrating from a non-composite schema.
Does this package work with `whereHas` or `orWhereHas`?
Yes, composite relations support `whereHas` and `orWhereHas` just like native Eloquent relations. For example: `$users->whereHas('compositeRole', fn($q) => $q->where('active', true))`. The package translates these queries into optimized SQL with composite key conditions.
What alternatives exist if I don’t want to use composite keys?
Consider surrogate keys (e.g., auto-increment `id`), intermediate tables (for many-to-many), or denormalization. For example, replace a composite `(user_id, role_id)` with a single `user_role_id` foreign key. Evaluate trade-offs like query complexity and data integrity.
Can I use this package with Laravel Scout or API resources?
Yes, composite relations work seamlessly with Scout and API resources. The relations are loaded like any other Eloquent relation, so they’ll appear in serialized output (e.g., JSON:API) or Scout search results. No additional configuration is needed.
How do I debug composite relation queries?
Use Laravel’s `toSql()` method to inspect generated queries: `$user->compositeRole->toSql()`. For complex cases, enable query logging in `config/database.php` or use tools like Laravel Debugbar. Composite queries may produce verbose SQL, so document your relation logic for the team.
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor