In Statamic, the taxonomies
method is used to define or retrieve taxonomies associated with a collection. While the documentation suggests this method works with arrays, it actually returns an Illuminate\Support\Collection
of Taxonomy
objects.
-
Inspecting Taxonomies:
Log::info(print_r($collection->taxonomies(), true));
This will show a
Collection
ofTaxonomy
objects, not a plain array. -
Common Pitfall: The following will not work because taxonomies are returned as objects:
$collectionTaxonomies = $collection->taxonomies(); $collectionTaxonomies[] = 'new_taxonomy_handle';
-
Correct Approach:
$collectionTaxonomies = $collection->taxonomies()->pluck('handle')->toArray(); $collectionTaxonomies[] = 'new_taxonomy_handle'; $collection->taxonomies($collectionTaxonomies); $collection->save();