Getting Started
Morphism provides enhanced polymorphic relationship management for Laravel with strict type enforcement, automatic morphing, and cleaner configuration.
Installation
Section titled “Installation”composer require cline/morphismBasic Setup
Section titled “Basic Setup”Service Provider
Section titled “Service Provider”'providers' => [ Cline\Morphism\MorphismServiceProvider::class,],Configuration
Section titled “Configuration”php artisan vendor:publish --tag=morphism-configreturn [ 'morphs' => [ 'commentable' => [ App\Models\Post::class, App\Models\Video::class, App\Models\Article::class, ], 'taggable' => [ App\Models\Post::class, App\Models\Product::class, ], ],];Basic Usage
Section titled “Basic Usage”use App\Models\Comment;use App\Models\Post;
// Create polymorphic relation$post = Post::find(1);$comment = new Comment(['body' => 'Great post!']);$post->comments()->save($comment);
// Query polymorphic relation$comments = Comment::where('commentable_type', Post::class)->get();Why Morphism?
Section titled “Why Morphism?”Problem: Default Laravel Morphs
Section titled “Problem: Default Laravel Morphs”// Without Morphism, morph maps use full class names// commentable_type = "App\Models\Post"
// This breaks if you rename/move classes// Couples database to PHP namespacesSolution: Morphism
Section titled “Solution: Morphism”// With Morphism, morph maps use clean aliases// commentable_type = "post"
// Rename/move classes freely// Database stays clean// Type enforcement prevents invalid relationsNext Steps
Section titled “Next Steps”- Basic Usage - Working with morphs
- Config-Based Setup - Configuration options
- Strict Enforcement - Type safety
- Migrations - Database setup
- Testing - Testing with Morpheus