Eager Loading Without Eloquent: Laravel Collection hasMany
The problem: You have two collections of plain arrays or objects and you need to associate them relationally, but you are not working with Eloquent models. Laravel's Collection class is powerful, but it has no built-in way to express a one-to-many relationship between two arbitrary datasets.
What It Is
laravel-collection-has-many is a small PHP library that registers a hasMany() macro on Laravel's Collection class. It lets you attach a related collection to a parent collection using foreign key and local key fields, exactly like Eloquent's eager loading, but for plain data. After calling $users->hasMany($posts, 'user_id', 'id', 'posts'), each user in the collection gains a posts property containing their matched items.
Why It Matters
This comes up more often than you'd think. API responses, CSV imports, cached datasets, cross-service joins — any time you're working with structured data outside of the ORM, you end up writing the same nested loop to group children under parents. This macro replaces that boilerplate with a single, readable call. It handles both arrays and objects, auto-wraps results in collections, and the key names are fully customizable.
Technical Takeaway
The implementation uses O(n+m) grouping instead of naive nested iteration. It indexes the child collection by foreign key in one pass, then iterates the parent collection and attaches matches by lookup. This is the same strategy Eloquent uses internally for eager loading — groupBy the foreign key first, then assign. If you ever need to optimize a manual join in collection-land, this pattern is worth stealing. View Code
