inani/lazy-belongs-to-many
Laravel package that adds a lazy-loading belongsToMany relationship, letting you defer large many-to-many queries until the collection is actually accessed. Keeps models light while still supporting pivot data, eager loading, and standard relationship methods.
Relation subclass to trim hydration overhead, or accept Laravel's stock belongsToMany and optimize elsewhere. Reading ~one file of source is cheaper than a design spike.belongsToMany only matters if you have a measured cost in pivot resolution — many small many-to-many lookups per request, wide pivot tables, or read-heavy endpoints that never touch pivot attributes. If profiling does not show BelongsToMany/pivot hydration in the top-N hot path, this decision resolves to "no" immediately and saves the team a quarter of yak-shaving.wherePivot constraints, and custom Pivot models? If yes, the stock relation wins outright and the question closes. If your pivots are pure join rows, a lighter relation becomes defensible.Relation/Query\Builder internals, which shift between majors. Adopting or forking this raises the cost of every Laravel upgrade, which is a roadmap input, not just an engineering detail.Consider it only when all of the following hold:
select narrowing, indexing the pivot table, and caching.wherePivot/orderByPivot constraints in the query layer.Look elsewhere — i.e., use stock belongsToMany — when any of these are true:
withPivot, wherePivot, pivot events, custom pivot models, polymorphic many-to-many, or chunk/cursor interop. Reimplementing these is how a "lightweight" package becomes a multi-sprint liability.Relation internals.To executives (one slide): "We evaluated a small open-source helper that trims overhead in one type of database relationship. It has negligible community adoption — 24 stars, zero downstream packages — so we are not recommending it as a dependency. The recommendation is a time-boxed spike: one engineer, a few days, read the source, measure against our own query profile. If the numbers do not justify it, we close the question and spend nothing further. If they do, we copy the relevant logic into our codebase, test it, and own it — no new vendor risk."
To engineering (the actual ask): Read the source before trusting it. Confirm supported Laravel versions, confirm it does not silently drop withPivot/wherePivot/eager-load behavior you depend on, and confirm the query it generates is the query you expect. Benchmark against belongsToMany on a representative dataset — not a toy table. If it wins, vendor a pinned, tested copy rather than adding the Composer dependency; if it loses or the gain is inside noise, write the finding down and move on.
Bottom line as PM: treat this as research input, not a build decision. The value here is the idea — a cheaper many-to-many relation — not the artifact. The artifact carries unmaintained-package risk with none of the ecosystem validation that normally offsets it. Fund the spike only if a hotspot already exists; otherwise the correct output of this assessment is "known option, not pursued," documented so the next person does not re-litigate it.
How can I help you explore Laravel packages today?