Using tags | 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)  Basic-usage  Using tags

 Version   v1      

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

  Using tags    
- [ 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 tags
==========

###  On this page 

1. [ Attaching tags ](#content-attaching-tags)
2. [ Detaching tags ](#content-detaching-tags)
3. [ Syncing tags ](#content-syncing-tags)
4. [ Managing tags ](#content-managing-tags)
5. [ Finding tags ](#content-finding-tags)
6. [ Getting types ](#content-getting-types)
7. [ Checking if a model has a tag ](#content-checking-if-a-model-has-a-tag)
8. [ Creating and deleting owners ](#content-creating-and-deleting-owners)

To make a saved model taggable, identify it with its morph alias and numeric primary key:

```
manager := tags.From(ctx)
owner := tags.Owner{Type: "posts", ID: post.ID}
```

Use a stable alias. Two models can have the same numeric ID without sharing tags when their aliases differ.

Attaching tags
--------------------------------------------------------------------------------------------------

Here's how you can add some tags:

```
err := manager.Attach(ctx, owner, tags.Names("tag 1"), tags.Selection{})
if err != nil {
    return err
}
err = manager.Attach(ctx, owner, tags.Names("tag 2", "tag 3"), tags.Selection{})
```

The tags are stored in the `tags` table. Existing names or slugs are reused, and a model has each tag attached only once. Mix saved tags with names using `[]tags.Ref{{ID: tag.ID}, {Name: "tag 4"}}`. A nonzero ID takes precedence over the name.

Detaching tags
--------------------------------------------------------------------------------------------------

Here's how tags can be detached:

```
err := manager.Detach(ctx, owner, tags.Names("tag 1", "tag 2"), tags.Selection{})
```

Unknown names are ignored. Unknown nonzero IDs return a `db.NotFoundError`. Detaching does not delete tags. `DetachAll(ctx, owner)` removes every assignment for that owner.

Syncing tags
--------------------------------------------------------------------------------------------

By syncing tags, the package makes sure only the tags given are attached to the model. All other tags, including typed tags, are detached.

```
err := manager.Sync(ctx, owner, tags.Names("tag 2", "tag 3"), tags.Selection{})
```

An empty list clears all assignments. Use `SyncWithType` to preserve other namespaces. All newly created tags and pivot changes roll back together when an operation fails, including failures from a custom pivot store.

Managing tags
-----------------------------------------------------------------------------------------------

Tags are stored in the `tags` table and can be managed with the manager:

```
tag, err := manager.Create(ctx, map[string]string{"en": "my tag"}, nil)
if err != nil {
    return err
}
tag.Name.V["en"] = "another tag"
if err := manager.Save(ctx, &tag); err != nil {
    return err
}

found, err := manager.Find(ctx, "another-tag", tags.Selection{})
if err != nil {
    return err
}
err = manager.Delete(ctx, &found)
```

`FindOrCreate(ctx, tags.Names("one", "two"), tags.Selection{})` resolves a batch, preserves input order and reuses repeated names. `Matching([]string{"one"}, tags.Selection{AnyType: true}).Get(ctx)` finds names or slugs in every namespace without creating tags.

Finding tags
--------------------------------------------------------------------------------------------

You can find all tags containing a specific value:

```
found, err := manager.Containing("on", tags.Selection{AnyType: true}).Get(ctx)
```

This matches `one`, `another-one` and `another-ONE-with-different-casing`, but not `two`. The search is case-insensitive. `%` and `_` are SQL wildcards, and values are bound parameters.

Getting types
-----------------------------------------------------------------------------------------------

Fetch all registered tag types with `manager.Types(ctx)`. The result contains distinct `*string` values, including nil for the untyped namespace. Non-null types are alphabetical. SQL dialects differ in where they sort NULL.

Checking if a model has a tag
-----------------------------------------------------------------------------------------------------------------------------------------------

```
has, err := manager.HasTag(ctx, owner, tags.Ref{Name: "tag 1"}, tags.Selection{})
```

You can pass an ID instead, or set `Selection.Type` to check a specific type. Unlike PHP's loaded-collection check, this checks the stored assignments and accepts either the localized name or slug.

Creating and deleting owners
--------------------------------------------------------------------------------------------------------------------------------------------

Create the model first, then attach tags in one `db.Transaction` when creation must be atomic. There is no attribute mutator or hidden queue for unsaved models. To detach tags when a model is deleted, add a native hook:

```
func (post *Post) Deleting(ctx context.Context) error {
    return tags.From(ctx).DetachAll(ctx, tags.Owner{Type: "posts", ID: post.ID})
}
```

Bulk deletes bypass native model hooks. Tag deletion cascades to assignments in the standard schema.

 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)
