Radical Design Course by Jack McDade

From the creator of Statamic

Learn how to make your websites standout and be remembered.

Bought Jack McDade's course on design. Going through it now...and it is SO well done!

— Justin Jackson, Transistor.fm

Taxonomy Term Repository

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 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.

// Example for a single site
$term->dataForLocale('default', [
'mandalorian_code' => 'This Is The Value',
//...
]);

When using multi-site, you can pass different data to each site.

$term->dataForLocale('default', $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
HR: Section
Learn More!

There is more to learn more in these related articles:

Tags

shipment-container

Repositories

HR: Section
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →