To work with the Repository Term queries, use the following Facade:
use Statamic\Facades\Term;
Methods
Methods | Description |
---|---|
all() |
Get all Terms |
find($id) |
Get Term by id |
findByUri($uri) |
Get Term by uri |
query() |
Query Builder |
make() |
Makes a new Term instance |
Querying
Examples
Get all tags
Term::query() ->where('taxonomy', 'tags') ->get();
Get all tags in a collection
Term::query() ->where('collection', 'blog') ->where('taxonomy', 'tags') ->get();
Get all tags in multiple collections
Term::query() ->where('taxonomy', 'tags') ->whereIn('collection', ['blog', 'news']) ->get();
Only include tags attached to entries
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.
$term = Term::make()->taxonomy('tags')->slug('my-term');
Data is stored on a term on a per site basis, even if you only are using a single site.
$term->dataForLocale('en', $data);$term->dataForLocale('fr', $frenchData);
You may call additional methods on the term to customize it further.
$term->blueprint('tag');
Finally, save it. It'll return a boolean for whether it succeeded.
$term->save(); // true or false