Using your own tag model | laravel-tags-go | Spatie       

 SPATIE  

laravel-tags-go
===============

spatie.be/open-source

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

 Version   v1      

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

  Using your own tag model    
- [ Introduction ](https://spatie.be/docs/laravel-tags-go/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-tags-go/v1/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-tags-go/v1/requirements)
- [ Installation and setup ](https://spatie.be/docs/laravel-tags-go/v1/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-tags-go/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-tags-go/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-tags-go/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-tags-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-tags-go/v1/releasing)
- [ Support us ](https://spatie.be/docs/laravel-tags-go/v1/support-us)

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

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

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

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

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

###  On this page 

1. [ Custom pivot behavior ](#content-custom-pivot-behavior)

You might want to override tag functionality or add extra fields and methods. Use your own native Laravel model through a typed adapter:

```
type CustomTag struct {
    tags.Tag
    Color string `json:"color"`
}

func (CustomTag) TableName() string { return "custom_tags" }

func (tag *CustomTag) Creating(ctx context.Context) error {
    tag.Color = "green"
    return nil
}
```

Register it instead of the default tag model:

```
tags.NewFor(tags.Adapter[CustomTag]{
    Table: "custom_tags",
    Key: "id",
    New: func(tag tags.Tag) CustomTag {
        return CustomTag{Tag: tag}
    },
    Fields: func(tag *CustomTag) *tags.Tag {
        return &tag.Tag
    },
})
```

Get the manager with `tags.FromFor[CustomTag](ctx)`. Creation, saving, lookup, assignment reads, query filters and deletion all use the chosen model. Returned results contain the complete `CustomTag`, including additional fields. Native `Creating`, `Updating` and `Deleting` hooks run as usual.

Keep `Adapter.Table` consistent with the native model's `TableName`, and `Adapter.Key` with the model's primary-key column. Names, slugs, types and ordering retain the standard column names. The adapter's `Fields` must expose the actual tag data, not a detached copy.

The default migrations create the standard tag columns with the configured numeric primary key. Add a migration for extra fields. For a pre-existing schema, set `Config.Migrate` to false and register your own migrations. Do not modify migrations already run in shared environments.

Custom pivot behavior
-----------------------------------------------------------------------------------------------------------------------

PHP's configurable pivot class maps to `Config.Pivots`, which accepts a `PivotStore`. Its `Query` method selects assignments for one owner. `Owners` selects owner keys of the given morph type assigned to any supplied tag ID. `Insert` and `Delete` implement writes and must use the supplied context so rollback works.

`Delete` with nil IDs means detach everything. An empty non-nil slice means detach nothing. Reads, checks, filters, sync and cleanup all use this store. For the default SQL layout, reuse or embed `tags.SQLPivots{Table: "taggables", MorphName: "taggable"}` and override the operations your application needs.

Change `Config.Table` and `Config.MorphName` to customize the assignment table and owner-column prefix without replacing its behavior. When your pivot needs a different schema, disable the default migrations and register your own. Tag deletion uses a cascading foreign key in the standard schema. Custom stores must provide their own tag-deletion cleanup when they do not use that constraint.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
