Retrieving tagged models | 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  Retrieving tagged models

 Version   v1      

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

  Retrieving tagged models    
- [ 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)

 Retrieving tagged models
========================

The package provides `Filter` with `Any`, `All` and `Without` match modes to help you find models with certain tags. It returns a composable native model query and never creates tags.

### With any tags

The `Any` mode returns models that have one or more of the given tags. Set `Selection.Type` to look for a specified type. Without it, only untyped tags match.

```
query, err := manager.Filter(ctx, model.Query[Post](), "posts.id", "posts",
    tags.Names("tag 1", "tag 2"), tags.Selection{}, tags.Any)
if err != nil {
    return err
}
posts, err := query.OrderBy("id").Get(ctx)
```

`posts.id` is the qualified owner key, and `posts` is the stored morph alias. These must describe the query's model. Use application-defined identifiers, not request input.

### With all tags

The `All` mode returns only models that have all the given tags. A non-existing name or a correct name with the wrong type matches no models.

```
query, err := manager.Filter(ctx, model.Query[Post](), "posts.id", "posts",
    tags.Names("tag 1", "tag 2"), tags.Selection{}, tags.All)
```

### With any tags of any type

Set `AnyType` to remove namespace restrictions on names and slugs:

```
query, err := manager.Filter(ctx, model.Query[Post](), "posts.id", "posts",
    tags.Names("tag 1", "tag 2"), tags.Selection{AnyType: true}, tags.Any)
```

### With all tags of any type

Combine `AnyType` with `All` to require every matching tag across types. If the same name identifies tags in two types, both tag IDs must be attached. A missing name matches no models.

```
query, err := manager.Filter(ctx, model.Query[Post](), "posts.id", "posts",
    tags.Names("tag 1", "tag 2"), tags.Selection{AnyType: true}, tags.All)
```

### Without tags and with tags of a type

Use `Without` to exclude owners with any matching tag. `FilterTypes(ctx, query, "posts.id", "posts", []string{"news", "blog"})` matches at least one tag in any listed type.

An empty `Any` list matches nothing. Empty `All` and `Without` lists leave the owner set unchanged. Unknown names are ignored for `Any` and `Without`. Unknown nonzero IDs return an error.

 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)
