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 Morph Where Has Laravel Package

rackbeat/laravel-morph-where-has

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Problem Solved: Addresses a critical gap in Laravel’s Eloquent ORM where whereHas() fails on polymorphic (morphTo) relationships, a common pain point in multi-model systems (e.g., invoices, orders, or media with shared owners).
  • Non-Invasive: Leverages Laravel’s existing query builder and morph system without requiring schema changes or breaking existing morphTo logic.
  • Use Case Alignment: Ideal for systems with polymorphic associations (e.g., Post::morphTo('author'), Invoice::morphTo('owner')) where filtering related models via whereHas is essential for performance or business logic.

Integration Feasibility

  • Low Friction: Single Composer dependency with zero manual configuration (auto-registers via Service Provider).
  • Backward Compatibility: Extends existing morphTo relationships without deprecating them; requires explicit opt-in via new method names (e.g., customer() instead of owner()).
  • Laravel Version Lock: Targets Laravel 5.8+ (per package metadata); verify compatibility with your stack (e.g., 8.x/9.x/10.x).

Technical Risk

  • Morph Class Enumeration: Requires developers to predefine all possible morphed classes in the model (e.g., forClass(App\Customer::class)). Missed classes will break whereHas for those types.
  • Query Overhead: Under the hood, this likely generates OR conditions for each morphed class, which could impact performance on large datasets or complex queries.
  • Testing Gaps: Limited adoption (0 dependents, 7 stars) suggests untested edge cases (e.g., nested whereHas, custom accessors, or dynamic morph maps).
  • Laravel Core Changes: If Laravel fixes this natively (e.g., via #33975), this package may become redundant.

Key Questions

  1. Morph Diversity: How many distinct morphed classes exist for each morphTo relationship in your system? High diversity increases maintenance burden.
  2. Query Complexity: Will whereHas be used in nested queries or joins? Performance may degrade with many morphed classes.
  3. Future-Proofing: Is your team aligned with Laravel’s roadmap? If Laravel resolves this natively, this package may need deprecation planning.
  4. Testing Coverage: Can you validate the package’s behavior with your specific morph configurations (e.g., custom keys, polymorphic constraints)?
  5. Alternatives: Have you considered workarounds like raw SQL or custom scopes for whereHas on morphs?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Perfect fit for Laravel applications using Eloquent ORM and polymorphic relationships (e.g., media libraries, CMS content, or e-commerce systems).
  • PHP Version: Compatible with PHP 7.4+ (Laravel 8.x/9.x/10.x); test with your PHP version.
  • Database Agnostic: Works with MySQL, PostgreSQL, SQLite, etc., as it operates at the query builder level.

Migration Path

  1. Assessment Phase:
    • Audit all morphTo relationships in your codebase.
    • Identify relationships where whereHas is critical (e.g., filtering invoices by customer type).
  2. Incremental Adoption:
    • Start with low-risk models (e.g., non-critical or rarely queried morphs).
    • Example: Replace Invoice::whereHas('owner') with Invoice::whereHas('customer') for App\Customer morphed owners.
  3. Testing:
    • Validate queries with and without the package (e.g., DB::enableQueryLog()).
    • Test edge cases: empty results, null morphs, and nested whereHas.
  4. Deprecation Planning:
    • If Laravel adds native support, plan to replace this package post-major version upgrade.

Compatibility

  • Laravel Versions: Confirmed for 5.8+; test thoroughly with your version (e.g., 9.x may need adjustments).
  • Package Conflicts: No known conflicts (MIT license, minimal codebase). Check for version constraints in composer.json.
  • Custom Morph Logic: If your app extends morphTo (e.g., custom constraints, polymorphic keys), test compatibility.

Sequencing

  1. Dependency Installation:
    composer require rackbeat/laravel-morph-where-has
    
  2. Model Updates:
    • Add new methods for each morphed class (e.g., customer(), vendor()).
    • Example:
      public function customer() {
          return $this->morphTo('owner')->forClass(App\Customer::class);
      }
      
  3. Query Updates:
    • Replace whereHas('owner') with whereHas('customer') where applicable.
    • Update tests and business logic layers.
  4. Performance Benchmarking:
    • Compare query execution times before/after integration, especially for complex morphs.

Operational Impact

Maintenance

  • Developer Burden: Requires explicit definition of morphed classes in every model, increasing boilerplate. Use traits or generators to mitigate.
  • Package Updates: Monitor for Laravel version compatibility (last release in 2022). Plan for manual testing on Laravel upgrades.
  • Documentation: Add internal docs for the new pattern (e.g., "Use whereHas('customer') instead of whereHas('owner') for polymorphic filtering").

Support

  • Debugging Complexity: Issues may arise from morph class mismatches or query generation. Log queries (DB::enableQueryLog()) to diagnose.
  • Community Support: Limited (7 stars, no dependents). Rely on GitHub issues or create internal runbooks for common scenarios.
  • Fallback Strategy: Document workarounds (e.g., raw SQL) for critical paths if the package fails.

Scaling

  • Performance: Query complexity grows linearly with the number of morphed classes. For >5 classes, consider:
    • Denormalizing morph type into a dedicated column (e.g., owner_type).
    • Using database views or materialized paths for frequent queries.
  • Caching: Leverage Laravel’s query caching if whereHas is used in high-traffic endpoints.
  • Database Load: Test with production-like datasets to ensure no N+1 query issues.

Failure Modes

  • Broken Queries: Forgetting to define a morphed class will silently fail whereHas for that type. Add runtime checks or tests.
  • Regression Risks: Laravel core changes (e.g., query builder optimizations) may break the package. Test after Laravel updates.
  • Schema Changes: If morph relationships evolve (e.g., new classes added), models must be updated proactively.

Ramp-Up

  • Onboarding: Train developers on the new pattern via:
    • Code examples for common morph use cases (e.g., users, orders).
    • Pair programming sessions for complex morphs.
  • Testing Strategy:
    • Unit tests for each morphed relationship method.
    • Integration tests for whereHas queries with varied data.
  • Rollout Phasing:
    • Start with non-critical features.
    • Monitor query performance and error rates post-deployment.
  • Training Materials:
    • Add a section to your internal Laravel guide on polymorphic whereHas.
    • Example: "To filter invoices by customer type, use whereHas('customer')."
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui