dmag's avatar
Level 6

Select models NOT related to a specific model in polymorphic relationship

I have a polymorphic one-to-many relationship of this structure:

class Comment extends Model
{
    public function commentable(): MorphTo
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    public function comments(): MorphMany
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

class Video extends Model
{
    public function comments(): MorphMany
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

I want to get comments that are not related to Video model, but the following doesn't seem to work for me:

Comment::whereDoesntHaveMorph('commentable', Video::class)->get();
0 likes
1 reply

Please or to participate in this conversation.