From 1298fb235593daa147d254d31e6798b67758f57f Mon Sep 17 00:00:00 2001 From: Fred Myerscough Date: Mon, 25 Sep 2023 08:47:07 +0100 Subject: [PATCH] feat(term) added taxonomy relationship to term --- src/Term.php | 10 ++++++++++ tests/TermTest.php | 12 +++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) 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); + } + +}