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 Has Many Merged

Laravel Has Many Merged Laravel Package

korridor/laravel-has-many-merged

Add a custom Eloquent hasManyMerged relationship to merge multiple hasMany relations into one collection. Query, eager load, sort, and paginate merged results as a single relation while keeping models and constraints intact.

View on GitHub
Deep Wiki
Context7

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships

Frequently asked questions about Laravel Has Many Merged
How do I merge two hasMany relationships (e.g., orders and returns) into a single collection in Laravel?
Use the `hasManyMerged` relationship by defining it on your model like `public function activity() { return $this->hasManyMerged([Order::class, Return::class]); }`. The package automatically combines results into one collection while preserving constraints and eager-loading support.
Does hasManyMerged support Laravel’s eager loading (e.g., with()) and constraints (e.g., whereHas)?
Yes, it fully integrates with Laravel’s eager loading via `with()` and supports constraints like `whereHas`. For example, `$user->with(['mergedActivity'])->whereHas('mergedActivity')->get()` works as expected, filtering merged results at the database level.
What Laravel and PHP versions does this package support?
The package is actively maintained for Laravel 10–12 and PHP 8.1–8.3. It aligns with Laravel’s deprecation policies and includes GitHub Actions to enforce compatibility, so you won’t encounter version conflicts if you’re using a supported stack.
Can I paginate or sort the merged results?
Yes, pagination and sorting are supported out of the box. Use `->paginate(10)` or `->orderBy('created_at', 'desc')` directly on the merged relationship, and the package handles the underlying SQL logic efficiently.
How does hasManyMerged handle polymorphic relationships (e.g., merging comments and replies across models)?
It supports polymorphic merging by default, assuming standard Laravel naming conventions (*_id/*_type). For custom keys or logic, use the `mergeUsing` method to define a closure that controls how results are combined or deduplicated.
Will this package work with large datasets (e.g., 50K+ rows) without performance issues?
For large datasets, performance depends on database-level filtering (e.g., `whereHas`) and pagination. The package optimizes merging at the SQL level, but you may need client-side pagination or additional indexing for datasets exceeding 100K rows to avoid memory strain.
Is there a way to customize how merged results are combined (e.g., prioritize certain fields or deduplicate)?
Yes, use the `mergeUsing` method to pass a closure that defines custom merge logic. For example, you can prioritize fields, deduplicate entries, or apply business-specific rules during the merge process.
Does hasManyMerged break existing hasMany relationships or require additional configuration?
No, it’s zero-configuration and low-coupling. The package registers itself via Laravel’s autoloader and doesn’t interfere with existing `hasMany` relationships. You can adopt it incrementally without disrupting your current queries or eager loads.
Are there alternatives to hasManyMerged for merging Eloquent relationships?
Manual merging with collections (e.g., `$model->orders->merge($model->returns)`) is an option but suffers from N+1 queries and lacks eager-loading support. Other packages like `spatie/laravel-activitylog` focus on auditing, not merging, so hasManyMerged is the most tailored solution for this use case.
How can I debug or inspect the SQL queries generated by hasManyMerged?
Use Laravel’s built-in debugging tools like `toSql()` or packages like `barryvdh/laravel-debugbar` to inspect the generated SQL. The package is designed to work seamlessly with these tools, making it easy to optimize queries or troubleshoot performance issues.
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony