Use morphTo() in laravel many to many polymorphic

476 Views Asked by At

How can I use morphTo() in many to many relationships?

Consider I've this four tables:

Posts:
 - id
 - title

Videos:
 - id
 - title

Tags:
 - id
 - name

Taggable:
 - id
 - tag_id
 - taggable_type
 - taggable_id

In other polymorphic types (like one2one or one2many) we can easily use $this->morhphTo() to define a taggable() method in our model which returns a list of either App\Post or App\Video:

class Tag extends Model
{
    public function taggable()
    {
        return $this->morphTo();
    }
}
// So when we call taggable(), it returns either App\Post or App\Video
// But what about using this way in many to many polymorphic relations?


I want something like this in my App\Tag model. Is this possible?

This problem has taken me 8 days so far and I still have no idea.

0

There are 0 best solutions below