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 Map Generator Laravel Package

spatie/laravel-morph-map-generator

Automatically generates and keeps Laravel Eloquent morph maps up to date. Models register themselves by implementing getMorphClass(), so you don’t forget to add entries. Optional auto-generation on app boot, configurable for custom model locations.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Polymorphic Relationships: The package directly addresses Laravel’s polymorphic relationship limitations by automating morph map registration, reducing manual configuration overhead.
  • Model-Driven Design: Fits seamlessly into Laravel’s Eloquent ecosystem, requiring minimal architectural changes (only getMorphClass() implementation).
  • Decoupling: Eliminates the need for manual morphMap definitions in AppServiceProvider, improving maintainability for teams with dynamic model registrations (e.g., plugins, modular apps).

Integration Feasibility

  • Low Barrier to Entry: Single method (getMorphClass()) per model; no database migrations or schema changes required.
  • Backward Compatibility: Works alongside existing morphMap configurations (can coexist or override).
  • Testing: CI/CD pipelines (GitHub Actions) include tests and static analysis (Psalm), reducing integration risk.

Technical Risk

  • Morph Map Conflicts: Risk of duplicate or conflicting keys if multiple models return the same getMorphClass() value (mitigated by explicit naming conventions).
  • Performance: Minimal runtime overhead (autoregistration happens once per model class load).
  • Laravel Version Lock: Package targets Laravel 10+; may require adjustments for older versions (check composer.json constraints).

Key Questions

  1. Model Naming Strategy: How will getMorphClass() values be standardized across the codebase (e.g., kebab-case, lowercase)?
  2. Existing Morph Maps: Does the app already use morphMap in AppServiceProvider? If so, how will conflicts be resolved?
  3. Dynamic Models: For apps with runtime-generated models (e.g., via plugins), will the package handle edge cases (e.g., model inheritance)?
  4. Testing Coverage: Are there existing tests for polymorphic relationships that need updating to account for auto-registration?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Native support for Eloquent models; no additional dependencies beyond Laravel core.
  • PHP Version: Compatible with PHP 8.1+ (aligns with Laravel 10+ requirements).
  • Tooling: Works with Laravel’s service container, package managers (Composer), and IDE autocompletion.

Migration Path

  1. Assessment Phase:
    • Audit existing morphMap usage in AppServiceProvider or config files.
    • Identify models lacking polymorphic support.
  2. Implementation:
    • Install package: composer require spatie/laravel-morph-map-generator.
    • Add getMorphClass() to target models (prioritize high-traffic polymorphic relationships).
    • Test polymorphic queries (e.g., hasMorphType, morphTo) in staging.
  3. Deprecation:
    • Gradually remove manual morphMap entries (if redundant) post-validation.
    • Document the transition in changelogs.

Compatibility

  • Laravel Versions: Tested on Laravel 10+; validate against your specific version (e.g., 11.x).
  • Third-Party Packages: Check for dependencies that manually register morph maps (e.g., laravel-medialibrary).
  • Custom Model Bindings: Ensure no conflicts with custom resolveModel() or resolveRouteBinding() logic.

Sequencing

  1. Phase 1: Implement getMorphClass() in core models (e.g., Post, Comment).
  2. Phase 2: Validate polymorphic queries in isolated tests.
  3. Phase 3: Roll out to plugin/module-specific models.
  4. Phase 4: Deprecate manual morphMap entries (if safe).

Operational Impact

Maintenance

  • Reduced Boilerplate: Eliminates manual morphMap updates when adding new models.
  • Centralized Configuration: No need to maintain a separate morph map file; logic lives in models.
  • Documentation: Update model documentation to highlight getMorphClass() requirements.

Support

  • Debugging: Easier to trace morph map issues to specific models (e.g., via dd($model->getMorphClass())).
  • Onboarding: New developers must only implement getMorphClass(); no need to explain morphMap quirks.
  • Error Handling: Package throws clear exceptions for missing getMorphClass() (e.g., MissingMorphClassMethod).

Scaling

  • Performance: Negligible impact on application startup (morph maps are cached).
  • Dynamic Scaling: Handles runtime model registrations (e.g., SaaS multi-tenancy with dynamic model classes).
  • Microservices: Useful in distributed systems where models are registered across services.

Failure Modes

  • Missing Method: Runtime errors if a model lacks getMorphClass() (mitigate with IDE hints or static analysis).
  • Key Collisions: Two models returning the same key (e.g., Post and BlogPost both returning 'post').
  • Caching Issues: Stale morph map caches in production (Laravel’s cache invalidation handles this).

Ramp-Up

  • Training: 15-minute session to explain getMorphClass() and polymorphic best practices.
  • Testing: Add integration tests for critical polymorphic relationships (e.g., Comment morphing to Post/Video).
  • Rollback Plan: Maintain manual morphMap as a fallback during transition.
  • Monitoring: Track polymorphic query failures in error logs (e.g., Class 'post' does not exist).
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport