This package provides use different morphMap
for different relationships.
If your polymorphic relation structures does not satisfy with each other, this package lets you use custom map for each relation definition.
PS: By default, Laravel does not support custom mapping for each relation.
composer require moneo/laravel-morphmap
class Post extends Model
{
// 🚀 Add HasCustomMorphMap trait!
use HasCustomMorphMap;
public function __construct(array $attributes = [])
{
// 👋 Custom definition for Category relation!
$this->customMorphMap = [
Category::class => 'post',
];
// 👋 Default for all others! (__CLASS__ definition is default. You don't need to add this.)
$this->defaultMorphType = Post::class;
parent::__construct($attributes);
}
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function categories(): MorphToMany
{
return $this->morphToMany(Category::class, 'categoryable');
}
}
This usage example covers the example data below.
Assume you use these tables in your project.
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
categories
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
categoryables
category_id - integer
categoryable_id - integer
categoryable_type - string
id | name |
---|---|
1 | Easy Seralization in Doctrine |
id | name |
---|---|
1 | Beyond Controllers in Laravel Projects |
id | name |
---|---|
1 | PHP |
2 | Laravel |
3 | Doctrine |
id | name |
---|---|
1 | Articles |
2 | Video Tutorials |
tag_id | taggable_id | taggable_type |
---|---|---|
1 | 1 | App\Models\Post |
3 | 1 | App\Models\Post |
1 | 1 | App\Models\Video |
2 | 1 | App\Models\Video |
category_id | categoryable_id | categoryable_type |
---|---|---|
1 | 1 | post |
3 | 1 | post |
1 | 1 | video |
2 | 1 | video |
Contributions are always welcome, thanks to all of our contributors!