Retrieving tagged models | laravel-tags | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-tags](https://spatie.be/docs/laravel-tags/v4)  Basic-usage  Retrieving tagged models

 Version   v4   v3

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

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

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

The package provides four scopes `withAnyTags`, `withAllTags`, `withAnyTagsOfAnyType`, and `withAllTagsOfAnyType` that can help you find models with certain tags.

### withAnyTags

The `withAnyTags` scope will return models that have one or more of the given tags attached to them. If you pass the `type` argument, it will look for tags with specified type, if not, it will only look for tags that have no type.

```
//returns models that have one or more of the given tags that are not saved with a type
YourModel::withAnyTags(['tag 1', 'tag 2'])->get();

//returns models that have one or more of the given tags that are typed `myType`
YourModel::withAnyTags(['tag 1', 'tag 2'], 'myType')->get();
```

### withAllTags

The `withAllTags` scope will return only the models that have all of the given tags attached to them. If you pass the `type` argument, it will look for tags with specified type, if not, it will only look for tags that have no type. So when passing a non-existing tag, or a correct tag name with the wrong type, no models will be returned.

```
//returns models that have all given tags that are not saved with a type
YourModel::withAllTags(['tag 1', 'tag 2'])->get();

//returns models that have all given tags that are typed `myType`
YourModel::withAllTags(['tag 1', 'tag 2'], 'myType')->get();
```

### withAnyTagsOfAnyType

The `withAnyTagsOfAnyType` scope will return models that have one or more of the given tags attached to them, but doesn't restrict given tags to any type if they are passed as `string`.

```
//returns models that have one or more of the given tags of any type
YourModel::withAnyTagsOfAnyType(['tag 1', 'tag 2'])->get();
```

### withAllTagsOfAnyType

The `withAllTagsOfAnyType` scope will return only the models that have all of the given tags attached to them, but doesn't restrict given tags to any type if they are passed as `string`. So when passing a non-existing tag no models will be returned.

```
//returns models that have all given tags of any type
YourModel::withAllTagsOfAnyType(['tag 1', 'tag 2'])->get();
```

 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)
