mashirou1234/laravel-when-on-relation
Adds a whenOnRelation helper to Laravel Eloquent so you can run callbacks or apply constraints only when a relationship is loaded or present. It keeps relation-aware models, queries, and API resources concise and free of repetitive checks.
Architecture fit: This package is a narrow, additive extension to Eloquent's query-callback surface. whenOnRelation() provides a relation-scoped variant of Laravel's when() conditional, and the release notes explicitly state that existing when() behavior is preserved. That makes it a low-coupling utility rather than a framework replacement — architecturally low risk to adopt, and easy to remove. The main architectural question is how it extends relations: a trait on relation classes, a macro registered on Illuminate\Database\Eloquent\Relations\Relation, or a wrapper. Macro-based is the most common Laravel pattern and the least invasive, but it also means the package's behavior is coupled to framework internals.
Integration feasibility: High. The feature is opt-in, so there is no forced change to existing query-building code. Any team already using when() on Eloquent builders will recognize the pattern immediately. Integration effort is essentially one service provider registration plus targeted use sites.
Technical risk: Primarily version-maturity risk, not architectural risk. v0.1.0 signals an unstable API and thin production hardening: no evidence in the release notes of a supported Laravel version matrix, test coverage, or compatibility guarantees. The Relation class hierarchy (HasMany, BelongsTo, BelongsToMany, MorphMany, etc.) varies meaningfully across Laravel majors, and macros registered on Relation inherit or miss subclasses depending on registration approach. There is also a non-trivial naming-collision risk: if Laravel adds a similarly named helper upstream, the macro could conflict or shadow framework behavior.
Key questions:
whenOnRelation() compose with when(), unless(), and chained relation callbacks?Stack fit: Fits standard Laravel applications using Eloquent relationship instances for API resources, scoped query composition, or conditional eager-loading logic. It is irrelevant for teams that build queries exclusively through repositories or the query builder without touching Relation objects — assess usage density of relations in the codebase before investing.
Migration path: Effectively zero-migration. Adoption is additive: install, register the provider (if required), and introduce whenOnRelation() at call sites opportunistically. No schema, config, or behavior changes to existing code paths. The safest pattern is to require new code to use it and leave existing when() calls untouched until there is a reason to change them.
Compatibility: The critical gating item. Because the helper targets Eloquent\Relation, compatibility must be verified against the exact Laravel major/minor versions in the fleet, including any LTS windows. Also check PHP version constraints and whether the package declares conflicts. If the application spans multiple Laravel versions across services, confirm the package supports the full range or restrict adoption to services on supported versions.
Sequencing:
when() behavior is genuinely unchanged and whenOnRelation() chains correctly on at least HasMany, BelongsTo, and a morph relation.Maintenance: The code footprint is small, so maintenance burden is low if the package is macro-based and does not patch vendor internals. The real maintenance risk is pre-1.0 API churn and abandonment. Mitigation: pin exact versions, wrap usage behind a project-local helper if usage becomes widespread, and keep adoption shallow enough to unwind quickly.
Support: v0.1.0 with no stated backing in the release notes is the weakest signal. Assume no SLA. Treat this as a "convenience dependency," not infrastructure — a service that must be upgradeable independently and replaceable within a sprint. Confirm there is an issue tracker and a security contact before production use.
Scaling: Runtime cost is negligible; conditional callbacks are evaluated at query-construction time and add no meaningful query overhead. The scaling concern is cognitive and organizational: parallel, near-identical APIs (when and whenOnRelation) invite inconsistency across teams. Publish a one-paragraph rule for which to use where, or the codebase will drift.
Failure modes:
when() and whenOnRelation() breaking fluent chains in unexpected places.Ramp-up: Low for any developer fluent in Eloquent — the API mirrors an existing pattern, so training is minutes, not days. The real ramp-up cost is documentation: at v0.1.0, expect the README to be the only reference and plan to write an internal usage note with the pilot's findings. Budget roughly half a day for the pilot engineer to validate behavior, document the chaining contract, and record the version matrix.
How can I help you explore Laravel packages today?