diff --git a/src/Models/Category.php b/src/Models/Category.php index 06b762a..b66c2c7 100644 --- a/src/Models/Category.php +++ b/src/Models/Category.php @@ -23,7 +23,7 @@ public static function boot() public function categorizables($class) { - return $this->morphedByMany($class, 'categorizable'); + return $this->morphedByMany($class, 'categorizable', 'categorizable', 'category_id'); } public function getRelationValue($key) @@ -34,7 +34,7 @@ public function getRelationValue($key) if (array_key_exists($key, config('categorizable.morphs', []))) { $class = config('categorizable.morphs')[$key]; - $relation = $this->morphedByMany($class, 'categorizable', 'categorizable', 'id'); + $relation = $this->categorizables($class); return tap($relation->getResults(), function ($results) use ($key) { $this->setRelation($key, $results); }); @@ -48,7 +48,7 @@ public function __call($method, $arguments) { if (array_key_exists($method, config('categorizable.morphs', []))) { $class = config('categorizable.morphs')[$method]; - return $this->morphedByMany($class, 'categorizable', 'categorizable', 'id'); + return $this->categorizables($class); } return parent::__call($method, $arguments); diff --git a/tests/CategoryTest.php b/tests/CategoryTest.php index ea7c7ec..bac4cca 100644 --- a/tests/CategoryTest.php +++ b/tests/CategoryTest.php @@ -193,4 +193,16 @@ public function testFindModelHasCategoriesByUnexistCategory() { $this->assertCount(0, TestModel::hasCategories('Nothing')->get()); } + + public function testListMorphs() + { + config()->set('categorizable.morphs', ['tests' => TestModel::class]); + $category = Category::create(['name' => 'news']); + $foo = TestModel::create(['title' => 'foo']); + $bar = TestModel::create(['title' => 'bar']); + $foo->categorize($category->id); + $bar->categorize($category->id); + $category->refresh(); + $this->assertCount(2, $category->load('tests')->tests); + } }