diff --git a/src/Term.php b/src/Term.php index eb59b2c..aa0eaf9 100644 --- a/src/Term.php +++ b/src/Term.php @@ -2,6 +2,8 @@ namespace Myerscode\Laravel\Taxonomies; +use Illuminate\Database\Eloquent\Relations\BelongsTo; + class Term extends Model { @@ -27,4 +29,12 @@ public static function addToTaxonomy($term, $taxonomy): self return new static; } + + /** + * Taxonomy associated with the term + */ + public function taxonomy(): BelongsTo + { + return $this->belongsTo(Taxonomy::class); + } } diff --git a/tests/TermTest.php b/tests/TermTest.php index 3bb33a2..4e4cc8b 100644 --- a/tests/TermTest.php +++ b/tests/TermTest.php @@ -3,6 +3,7 @@ namespace Tests; use Myerscode\Laravel\Taxonomies\Exceptions\UnsupportedModelDataException; +use Myerscode\Laravel\Taxonomies\Taxonomy; use Myerscode\Laravel\Taxonomies\Term; class TermTest extends TestCase @@ -62,4 +63,13 @@ public function testAddTermToTaxonomy() $this->assertCount(2, Term::all()); } -} \ No newline at end of file + public function testTermBelongsToTaxonomy() + { + Term::addToTaxonomy('Hello', 'World'); + $term = Term::findByName('Hello'); + + $this->assertInstanceOf(Taxonomy::class, $term->taxonomy); + $this->assertEquals('World', $term->taxonomy->name); + } + +}