Download the PHP package moneo/laravel-morphmap without Composer
On this page you can find all versions of the php package moneo/laravel-morphmap. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download moneo/laravel-morphmap
More information about moneo/laravel-morphmap
Files in moneo/laravel-morphmap
Package laravel-morphmap
Short Description Custom morphMap support for Laravel Framework
License MIT
Informations about the package laravel-morphmap

Laravel Custom Morph Map
Use different morph maps for different relationships on the same model.
By default, Laravel's Relation::morphMap() applies globally to all polymorphic relationships. This package lets you define a custom morph map per relationship, so different polymorphic relations on the same model can use different type aliases.
Requirements
| Version | PHP | Laravel |
|---|---|---|
| 2.x | ^8.1 | 10, 11, 12 |
| 1.x | >=8.0 | 8, 9, 10, 11 |
Installation
No service provider registration is needed. Just use the trait.
Usage
Add the HasCustomMorphMap trait to your model and define the $customMorphMap property in your constructor.
The array keys are the related model's fully qualified class name, and the values are the morph type alias you want stored in the database for that relationship:
Inverse Relationships (morphedByMany)
The trait also supports morphedByMany for inverse polymorphic many-to-many relationships:
How It Works
The trait uses Laravel's initializeHasCustomMorphMap() convention to automatically set the default morph type when the model is constructed. No need to call parent::__construct() before setting properties -- the trait handles initialization automatically.
When you call morphToMany() or morphedByMany(), the trait:
- Looks up the related model class in your
$customMorphMaparray - If found, registers that alias in Laravel's global morph map
- If not found, uses the
$defaultMorphType(which defaults to the model's FQCN) - Delegates to the parent relationship method
Example Scenario
Suppose your taggables table uses fully qualified class names, but your categoryables table uses short aliases:
taggables
| tag_id | taggable_id | taggable_type |
|---|---|---|
| 1 | 1 | App\Models\Post |
| 2 | 1 | App\Models\Video |
categoryables
| category_id | categoryable_id | categoryable_type |
|---|---|---|
| 1 | 1 | post |
| 2 | 1 | video |
With this package, both table structures work seamlessly on the same model.
Testing
Static Analysis
Code Style
Contributing
Contributions are always welcome! Please see CONTRIBUTING.md for details.
Thanks to all of our contributors!
License
The MIT License (MIT). Please see License File for more information.