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

spatie/laravel-morph-map-generator

Stop forgetting to register models in your Laravel morph map. This package auto-generates morph maps at boot, letting each model register itself via a simple getMorphClass method. Includes configurable autogeneration and a base directory setting for non-default model locations.

View on GitHub
Deep Wiki
Context7

spatie/laravel-morph-map-generator automatically builds your Laravel morph map, so you don’t need to manually register every model. Implement getMorphClass() on each model, and the package discovers and registers it automatically.

  • Automatic generation when the application boots
  • Zero manual morph map maintenance
  • Configurable base model directory
  • Simple getMorphClass() integration
  • Publishable config for project-specific setup
Frequently asked questions about Laravel Morph Map Generator
How do I install spatie/laravel-morph-map-generator in a Laravel app?
Install it with composer require spatie/laravel-morph-map-generator, then publish the config using the vendor:publish command for the MorphMapGeneratorServiceProvider config tag. Implement getMorphClass() on the models you want included, and the package will register their aliases during service-provider boot. The assessment notes support for Laravel 9 through 13 and PHP 8.1 through 8.4, so pin the package version and verify it against your exact stack.
How does laravel-morph-map-generator build the morph map automatically?
At service-provider boot, the package scans the configured model paths and reads each model's getMorphClass() return value to register the alias in Laravel's morph map. The autogenerate config option controls whether this happens automatically on application boot. You can also generate and cache the map explicitly rather than relying on boot-time scanning.
Do I need to implement getMorphClass() on every Eloquent model?
Yes for every model you expect in the generated morph map, because the package derives aliases from each model's getMorphClass() return value. A common approach is implementing it on a base model, then using base_models filtering and ignored_models to control which models are included. The alias must be stable and match historical morph_type values already stored in the database.
Which Laravel and PHP versions does this morph map package support?
The package advertises support across Laravel 9 through 13 and PHP 8.1 through 8.4, which should cover most maintained Laravel applications. You should still validate Composer constraints and run your polymorphic relationship tests on the exact Laravel and PHP versions you deploy. If you use Laravel 11+ optimization commands, confirm how optimize and optimize:clear interact with your cached morph map.
How do I cache the generated morph map for production deployments?
Run php artisan morph-map:cache as part of your deployment process and disable autogenerate in production so the application uses the cached map instead of scanning models on every boot. Make sure the cache is cleared or rebuilt consistently across all nodes, especially in multi-node deployments where a filesystem cache is not shared. The package supports both filesystem and Laravel cache drivers, so verify the configured driver works in your environment.
Why is an Eloquent model missing from the generated morph map?
The model may be outside the configured model paths, filtered out by base_models or ignored_models, or skipped because class loading failed during discovery. A stale cached map after deployment can also make models appear missing. Check discovery output, rebuild the cache, and explicitly register vendor or package models in Laravel's morph map when they are not inside your configured app paths.
Can I change a morph alias after polymorphic data already exists?
Changing a morph alias is a data migration, not just a config change, because aliases are persisted in database morph_type columns. Implement getMorphClass() to return the existing persisted alias rather than a new preferred name, and review alias changes carefully. Keep a rollback plan in case a generated map omits or renames a model used by production data.
How should I handle vendor or package models with laravel-morph-map-generator?
Models owned by vendor packages may not live under your configured application paths, so the generator will not discover them by default. Define those models explicitly in Laravel's Relation::morphMap() and test the merge behavior with the generated map. The assessment recommends this approach when polymorphic models are primarily in vendor packages or dynamically generated.
Does autogenerate on application boot create performance or production risks?
Autogenerate scans configured model paths during service-provider boot, which adds overhead and can surface class-loading failures during discovery. It is generally safer to disable autogenerate in production and rely on a cached map built during deployment. Keep autogeneration for local or CI environments, and include morph-map:cache in your deployment sequence.
What are alternatives to spatie/laravel-morph-map-generator for managing morph maps?
You can maintain Laravel's Relation::morphMap() manually, build a custom service provider that scans models, or enforce getMorphClass() on a base model with fail-fast behavior for omissions. This package is a narrow tactical utility with limited external validation, so treat it as a convenience rather than a platform-critical dependency unless your team accepts ownership of its behavior. An alias audit and polymorphic test suite are more important than the generator itself.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections