sebastiaanluca/laravel-auto-morph-map
App\Modules\Blog\Post → Blog\Post) without breaking polymorphic relationships in the database. Critical for large-scale applications with modular architectures (e.g., microservices, monoliths with domain-driven design).App\User to Auth\User or Domain\User) while maintaining backward compatibility with legacy data.morphMap configurations in AppServiceProvider), reducing technical debt and maintenance overhead.App\OldUser → App\NewUser) without data migration.Tenant1\Post and Tenant2\Post → both map to Post).Commentable → Article or Video instead of App\Modules\Articles\Article).User, Post) in unit tests without polluting the global namespace.Adopt When:
Comment belongs to Post or Video) and you need to decouple database types from class namespaces.morphMap updates across the codebase.morphMap array in a service provider).Look Elsewhere If:
morphMap is sufficient).spatie/laravel-polymorphic-relations or custom solutions)."This package lets us simplify and future-proof our database relationships without costly migrations. For example, if we rename App\Blog\Post to Content\Post for better organization, this tool automatically updates the database to reflect the change—saving weeks of manual work and reducing risk. It’s a low-cost, high-impact way to clean up technical debt while keeping our backend flexible for future refactoring."
Key Benefits:
*"This package automates Laravel’s polymorphic type mapping, replacing manual morphMap configurations with a declarative, scalable approach. Instead of hardcoding App\Modules\Blog\Post in every polymorphic relationship, we define short aliases (e.g., BlogPost) in a single config file. This is especially useful for:
Trade-offs:
Recommendation: Pilot this for a non-critical polymorphic relationship (e.g., comments on posts/videos) to validate the workflow before rolling out broadly."*
Implementation Steps:
config/automorphmap.php (e.g., App\Models\Blog\Post → BlogPost).morphMap in AppServiceProvider with the package’s auto-mapping.Comment::where('commentable_type', 'BlogPost')) work as expected.How can I help you explore Laravel packages today?