cviebrock / eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
MIT License
537 stars 72 forks source link

not work getAllTags methods with morphMap #120

Closed sergey-yabloncev closed 3 years ago

sergey-yabloncev commented 3 years ago

If add model in service proveder morphMap Relation::morphMap([ 'model' => Model::class, ]); Methods Model::allTags()and $tagService->getAllTags(\App\Model); not find tags


class DataBaseServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        // Set or get the morph map for polymorphic relations
        Relation::morphMap([
            'model' => Model::class,
        ]);
    }
}
cviebrock commented 3 years ago

I'm not actually sure what your service provider is doing here. That said, there seem to be some alternate solutions here that might solve this for you: https://github.com/laravel/framework/issues/17830

sergey-yabloncev commented 3 years ago

Hello @cviebrock there https://github.com/cviebrock/eloquent-taggable/blob/master/src/Services/TagService.php#L217 you use where by class, but if you use morphMap for Model, in database tag saving with morph name https://i.imgur.com/EUZsXGQ.png instead of namespace class

And if you try use not a namespace class for example with morhmap name $tagService->getAllTags('stores') you get exception https://i.imgur.com/zhJfp4J.pnt

cviebrock commented 3 years ago

I'm still not 100% sure what's going on in your case. If you are overloading the Morph model used by Laravel for polymorphic relations, then it's going to overload the Tag model class used by the package internally. You'd need to find a way to prevent that.

Alternatively, if you can find a way to do it (by changing the logic in the getAllTags method), then please submit a PR and I'll update the package.