To work with the with Taxonomy
Repository, use the following Facade:
use Statamic\Facades\Taxonomy;
Methods
Methods | Description |
---|---|
all() |
Get all Taxonomies |
find($id) |
Get Taxonomy by id |
findByHandle($handle) |
Get Taxonomy by handle |
findByUri($mount) |
Get Taxonomy by uri |
findOrFail($id) |
Get Taxonomy by id . Throws a TaxonomyNotFoundException when the taxonomy cannot be found. |
handleExists($handle) |
Check to see if Taxonomy exists |
handles() |
Get all Taxonomy handles |
queryTerms() |
Query Builder for Terms |
make() |
Makes a new Taxonomy instance |
The id
is the same as handle
while using the default Stache driver.
Querying
While the Taxonomy
Repository does not have a Query Builder, you can still query for Terms inside Taxonomies with the queryTerms
method. This approach can be useful for retrieving Terms with an existing Taxonomy object.
$tags = Taxonomy::find('tags'); $tags->queryTerms()->get();
When a taxonomy can't be found, the Taxonomy::find()
method will return null
. If you'd prefer an exception be thrown, you may use the findOrFail
method:
Taxonomy::findOrFail('tags');
Creating
Start by making an instance of a taxonomy with the make
method. You can pass the handle into it.
$taxonomy = Taxonomy::make('tags');
You may call additional methods on the taxonomy to customize it further.
$taxonomy ->title('Tags') ->cascade($data) // an array ->revisionsEnabled(false) ->searchIndex('tags') ->defaultPublishState('published') // or 'draft' ->sites(['one', 'two']) // array of handles
Finally, save it.
$taxonomy->save();