You can create a simple term using the add
helper method on the Myerscode\Laravel\Taxonomies\Term
model, and passing in a name.
Term::add('Foo');
A slug of the name will be created for you, however an alternative slug can be set by passing it in a data array.
Term::add(['slug' => 'bar', 'name' => 'Foo']);
You can add a single term:
$model->addTerm('Foo');
$model->addTerm('Bar');
// $model would now have the tags Foo and Bar
You can add a multiple term:
$model->addTerms(['Hello', 'World']);
// $model would now have the tags Hello and World
You can sync term:
$model->syncTerms('Foo');
// $model would now only have the tag Foo
$model->syncTerms(['Hello', 'World']);
// $model would now only have the tags Hello and World
You can remove terms:
$model->detachTerms(['Hello', 'World']);
$model->detachTerms('Foo');
By default terms do not get added to a taxonomy, but you can associate a new term or move association of an existing term to any taxonomy.
$tag = Term::create('Foo');
$taxonomy->attachTerms($tag);
$anotherTaxonomy->attachTerms($tag);
$tag = Term::find('Foo');
$anotherTaxonomy->attachTerms($tag);
The $tag with name Foo
is now associated to the $anotherTaxonomy
.
When you have a term object you can get all the information associated with it!
Call the taxonomy
property and this an instance of Term
or will be null
if it has not been assigned to it.
$taxonomy = $term->taxonomy;