# Taxonomy Term Repository

To work with the Repository Term queries, use the following Facade:

```php
use Statamic\Facades\Term;
```

## Methods

| Methods                  | Description                                                                       |
| ------------------------ | --------------------------------------------------------------------------------- |
| `all()`                  | Get all Terms                                                                     |
| `find($id)`              | Get Term by `id`                                                                  |
| `findByUri($uri)`        | Get Term by `uri`                                                                 |
| `findOrFail($id)`        | Get Term by `id`. Throws a `TermNotFoundException` when the term cannot be found. |
| `findOrMake($id)`        | Get Term by `id` or make a new term.                                              |
| `findOr($id, $callback)` | Get Term by `id` or execute callback.                                             |
| `query()`                | Query Builder                                                                     |
| `make()`                 | Makes a new `Term` instance                                                       |

## Querying

#### Examples {.popout}

#### Get a single term by its id

When getting a single term by its ID, the value of the `$id` parameter should be `taxonomy_handle::term_id`.

```php
Term::query()->where('id', 'tags::123')->first();

// Or with the shorthand method
Term::find('tags::123');
```

When a term can't be found, the `Term::find()` method will return `null`. If you'd prefer an exception be thrown, you may use the `findOrFail` method:

```php
Term::findOrFail('tags::123');
```

#### Get all tags

```php
Term::query()
    ->where('taxonomy', 'tags')
    ->get();
```

#### Get all tags in a collection

```php
Term::query()
    ->where('collection', 'blog')
    ->where('taxonomy', 'tags')
    ->get();
```

#### Get all tags in multiple collections

```php
Term::query()
    ->where('taxonomy', 'tags')
    ->whereIn('collection', ['blog', 'news'])
    ->get();
```

#### Only include tags attached to entries

```php
Term::query()
    ->where('taxonomy', 'tags')
    ->where('entries_count', '>=', 1);
    ->get();
```


## Creating

Start by making an instance of a term with the `make` method.
You need at least a slug and the taxonomy.

```php
$term = Term::make()->taxonomy('tags')->slug('my-term');
```

Data for a term is stored on a per site basis, even if you only are using a single site.

The method expects a site handle and an array of key-value pairs.
```php
// Example for a single site
$term->dataForLocale('default', [
    'mandalorian_code' => 'This Is The Value',
    //...
]);
```

When using [multi-site](/multi-site.md), you can pass different data to each site.
```php
$term->dataForLocale('default', $data);
$term->dataForLocale('fr', $frenchData);
```

You may call additional methods on the term to customize it further.

```php
$term->blueprint('tag');
```

Finally, save it. It'll return a boolean for whether it succeeded.

```php
$term->save(); // true or false
```
