Weave 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 Merged Relations

Laravel Merged Relations Laravel Package

staudenmeir/laravel-merged-relations

Merge multiple Laravel Eloquent relations into a single relation that can be queried and eager loaded as one. Combine hasMany, belongsToMany, and morph relations into a unified result set, simplifying complex relationship handling in your models.

View on GitHub
Deep Wiki
Context7

Merged Laravel Eloquent relationships

Frequently asked questions about Laravel Merged Relations
What does staudenmeir/laravel-merged-relations do?
It adds an Eloquent trait and relation method that let you combine several relations that resolve to the same related model, such as multiple hasMany, belongsToMany, or morph relations, into one relation. You can then query and eager load that merged relation as a single result set. It is additive and does not require a service provider, config file, migration, or schema change.
How do I install and set up staudenmeir/laravel-merged-relations?
Install it with Composer using composer require staudenmeir/laravel-merged-relations. Then apply the package trait to the model that owns the constituent relations and define a method that returns the merged relation. There is no service provider to register and no config to publish, but check the README for the exact trait and method names for your package version.
Can I merge hasMany and belongsToMany relations into one Eloquent relation?
Yes, the package is designed to merge relations that resolve to the same related model, including hasMany and belongsToMany relations. The merged relation can be queried and eager loaded as one relation. It does not turn arbitrary cross-table queries into a relation, so the constituent relations need a compatible related model and result shape.
Does laravel-merged-relations support morph relations such as morphMany?
The package overview lists morph relations among the relation types that can be combined, so morphMany-style relations that resolve to the same related model are in scope. As with any polymorphic setup, verify the generated SQL and result ordering for your exact type columns and constraints. Integration tests should cover ordering, pagination boundaries, and type filtering.
How do I eager load a merged relation in Laravel?
Define the merged relation as a method on your model, then eager load it like any other Eloquent relation, for example with with('mergedRelationName'). The purpose of the package is to let the merged relation participate in Eloquent's normal query and eager-loading APIs rather than forcing a separate query-builder path. Confirm that eager-loaded results preserve the ordering and constraints you expect.
Can I paginate or use whereHas on a merged relation?
A merged relation is intended to be queryable like a normal Eloquent relation, so pagination and relationship existence queries such as whereHas are central use cases. Because the package assembles more complex SQL than a plain relation, explicitly test pagination boundaries and whereHas behavior for each merged relation. If you are replacing hand-rolled UNION code that mishandled pagination or withCount, those paths deserve validation first.
Which Laravel versions does staudenmeir/laravel-merged-relations support?
Support follows the package's Composer constraint and release history; the assessment notes that v1.11 was a compatibility-only patch for a new Laravel major. Pin the package release that matches your current Laravel version and check the README or composer.json before upgrading the framework. Treat it as an explicit item on your Laravel upgrade checklist because it reaches into Eloquent internals.
Is staudenmeir/laravel-merged-relations safe for production?
It is low-to-moderate risk: the integration is small and removable, but it couples to Eloquent internals such as Relation and existence-query construction. The main production concern is not a crash but a silently plausible wrong result set, so you own tests for ordering, pagination, and counts on each merged relation. Single-maintainer concentration also means it should be tracked on framework-upgrade paths.
How does it perform compared with separate queries or a UNION?
The package generates more complex SQL than a plain relation, so performance should be measured rather than assumed. It is usually most compelling when replacing fragile hand-rolled UNION queries or multiple PHP-side merges that already struggle with pagination, withCount, or whereHas. If it replaces one clean hasMany, the extra dependency and SQL complexity are probably not justified.
What are alternatives to merging Eloquent relations?
Alternatives include separate relation queries merged in PHP, hand-written UNION query-builder code, or a dedicated polymorphic activities or feed table. The package is a narrow fit for relations that resolve to the same related model, not a replacement for a properly modeled cross-entity feed. If you are deliberately deferring a polymorphic table, document the date or metric that reopens that decision.
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.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource