belongsTo(Specie::class); } public function Articles(): MorphMany { return $this->morphMany(Article::class, 'product'); } public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'taggable'); } public function scopeByTag($query, $tagId) { return $tagId ? $query->whereHas('tags', function ($query) use ($tagId) { $query->byId($tagId); })->orWhereHas('Specie', function($query) use ($tagId) { $query->byTag($tagId); }) : $query; } public function scopeByTags($query, $tags) { return $tags ? $query->whereHas('tags', function ($query) use ($tags) { $query->byIds($tags); })->orWhereHas('Specie', function($query) use ($tags) { $query->byTags($tags); }) : $query; } }