The normal use of polymorphic relationships in Laravel is pretty well covered by the Post-User-Image example.
I am trying to find a clean way to implement relationships, to say that the relationship is Article / ContentA / ContentB.
articles id content_1 id content_2 id user_defined_content_n id contentables article_id contentable_id contentable_type
Content classes are not necessarily known in the article, so defining an article model with many MorphedByMany relationships MorphedByMany not the way I want to do this.
Perhaps I am badly structuring my classes. I could create a ContentEntity class that translates to separate Content classes, but I would like to avoid this if possible.
Perhaps this better explains my problem.
class Article extends Model { public function contentEntities() { return $this->hasMany(ContentEntity::class); } } class ContentEntity extends Model { public function contentable() { return $this->morphTo(); } } class Content extends Model { public function contentEntity() { return $this->morphOne(ContentEntity::class, 'contentable'); } } class Video extends Model { public function contentEntity() { $this->morphOne(ContentEntity::class, 'contentable'); } }
It works, but it seems very unclean to me. I think it adds too much overhead for developers to manage the ContentEntity parent.
Edit: If someone does not provide a better solution, I went with an EAV style solution using ContentEntity s.
inheritance php laravel laravel-5
Sturm
source share