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

Laravel Has Many Merged Laravel Package

korridor/laravel-has-many-merged

Adds a hasManyMerged relationship to Laravel Eloquent, letting you combine several hasMany relations across models into one query-backed collection with sorting, limits, and eager loading. Ideal for unified feeds, timelines, and activity lists.

View on GitHub
Deep Wiki
Context7

korridor/laravel-has-many-merged adds a merged relationship to Laravel Eloquent, letting a single model combine several hasMany relations—across different models or constraints—and query, lazy load, or eager load them as one collection. It builds on SQL UNION ALL so all related rows are fetched in a single efficient query, keeping models clean and avoiding N+1 overhead.

  • Define one merged relation from multiple hasMany sources
  • Full support for eager loading (with) and lazy loading
  • Apply per-relation query callbacks and constraints
  • Returns standard Eloquent collections for familiar handling
  • Works with existing models and Laravel's relationship API
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.
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel