php - Use morphTo() in laravel many to many polymorphic
Get the solution ↓↓↓How can I usemorphTo()
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 ataggable()
method in our model which returns a list of eitherApp\Post
orApp\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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.