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

Laravel package adding a “hasManyMerged” relationship to combine results from multiple hasMany relations into one merged collection/query. Useful for aggregating related models across different types or sources while keeping a familiar Eloquent API.

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 multiple `HasMany` relations into a single Eloquent relationship in Laravel?
Use the `mergedHasMany` method provided by the package. For example, `return $this->mergedHasMany(['posts', 'comments'], 'created_at', 'desc');` merges `posts` and `comments` relations into one collection, ordered by `created_at`. The method accepts an array of relation names, a sort column, and direction.
Does `korridor/laravel-has-many-merged` support eager loading to avoid N+1 queries?
Yes, it fully supports Laravel’s eager loading. Use `with('merged_relation_name')` to load the merged collection in a single query, just like standard Eloquent relations. This reduces database queries significantly, especially for APIs or complex dashboards.
Can I apply `where`, `orderBy`, or other query constraints to the merged relation?
Absolutely. The merged relation behaves like a standard Eloquent relation, so you can chain methods like `where('type', 'post')`, `orderBy('title')`, or use scopes. For example: `user->mergedPosts()->where('status', 'published')->get()`. It also supports Laravel’s `fn()` syntax for fluent constraints.
Which Laravel versions and PHP versions does this package support?
The package supports Laravel 9 through 13 and PHP 8.1 to 8.3. It’s designed to work seamlessly with modern Laravel stacks, including Eloquent’s latest features like `fn()` for query constraints. Always check the package’s changelog for updates on newer Laravel releases.
How does pagination work with merged relations? Will `paginate()` return accurate results?
Pagination with `paginate()` may return inaccurate offsets due to the underlying UNION logic. For reliable pagination, use `simplePaginate()` or cursor-based pagination. The package recommends avoiding `paginate()` for merged relations where exact offsets are critical, as it can lead to inconsistent results.
Can I customize the merging logic, like deduplicating records or reordering results?
Yes, the package provides a `mergeResults` method to customize how merged data is processed. For example, you can deduplicate records, reorder results, or apply additional logic before returning the collection. This is useful for edge cases where default merging behavior isn’t sufficient.
Is there a performance impact when merging multiple relations, especially with large datasets?
Merging relations introduces some overhead due to UNION ALL or subquery logic, which can impact performance with large datasets or deeply nested queries. For optimal performance, test with your database (e.g., PostgreSQL vs. MySQL may handle UNION differently) and consider merging only the most frequently accessed relations.
How do I debug or inspect the SQL query generated by `mergedHasMany`?
Use Eloquent’s `toSql()` method to inspect the generated query. For example, `user->mergedPosts()->toSql()` will output the raw SQL, helping you identify issues or optimize performance. This is especially useful when debugging complex constraints or ordering.
Are there alternatives to `korridor/laravel-has-many-merged` for merging relations in Laravel?
Alternatives include manually merging collections in model accessors or using raw SQL with UNION queries. However, those approaches lack the ergonomics and query builder integration of this package. For example, manually merging requires additional code and doesn’t support eager loading or fluent constraints as seamlessly.
How do I integrate this package into an existing Laravel project without breaking changes?
Integration is straightforward: add the package via Composer (`komposer require korridor/laravel-has-many-merged`), then use the `HasManyMerged` trait in your models. Start with non-critical models to test performance and edge cases. The package is designed for incremental adoption and doesn’t require schema changes or migrations.
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.
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
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai