Using your own tag model | laravel-tags | Spatie

 SPATIE

  Laravel Tags
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-tags](https://spatie.be/docs/laravel-tags/v4)  Advanced-usage  Using your own tag model

 Version   v4   v3

 Other versions for crawler [v4](https://spatie.be/docs/laravel-tags/v4) [v3](https://spatie.be/docs/laravel-tags/v3)

- [ Introduction ](https://spatie.be/docs/laravel-tags/v4/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-tags/v4/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-tags/v4/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-tags/v4/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-tags/v4/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-tags/v4/changelog)
- [ About us ](https://spatie.be/docs/laravel-tags/v4/about-us)

Basic usage
-----------

- [ Using tags ](https://spatie.be/docs/laravel-tags/v4/basic-usage/using-tags)
- [ Retrieving tagged models ](https://spatie.be/docs/laravel-tags/v4/basic-usage/retrieving-tagged-models)

Advanced usage
--------------

- [ Adding translations ](https://spatie.be/docs/laravel-tags/v4/advanced-usage/adding-translations)
- [ Using types ](https://spatie.be/docs/laravel-tags/v4/advanced-usage/using-types)
- [ Sorting tags ](https://spatie.be/docs/laravel-tags/v4/advanced-usage/sorting-tags)
- [ Using your own tag model ](https://spatie.be/docs/laravel-tags/v4/advanced-usage/using-your-own-tag-model)
- [ Using another default locale ](https://spatie.be/docs/laravel-tags/v4/advanced-usage/using-another-default-locale)

 Using your own tag model
========================

You might want to override some functionality of the `Spatie\Tags\Tag` or add some extra functions. It's very easy to use your own custom tag model. All you need to do is override the `getTagClassName` method from the `HasTags` trait. It should return the fully qualified class name of an eloquent model that extends `Spatie\Tags\Tag` and uses the same `tags` table.

```
use Illuminate\Database\Eloquent\Model;
use Spatie\Tags\HasTags;

class YourModel extends Model
{
    use HasTags;

    public static function getTagClassName(): string
    {
        return YourTagModel::class;
    }
}
```

Then you need to override the `tags()` method from the same trait to tell Laravel that it still needs to look for `tag_id` column for tags relation instead of `your_tag_model_id`:

```
use Illuminate\Database\Eloquent\Relations\MorphToMany;

public function tags(): MorphToMany
{
    return $this
        ->morphToMany(self::getTagClassName(), 'taggable', 'taggables', null, 'tag_id')
        ->orderBy('order_column');
}
```
