Skip to content

Commit

Permalink
fix morphs
Browse files Browse the repository at this point in the history
  • Loading branch information
FreedomKnight committed Jun 26, 2018
1 parent ab94965 commit b77a2db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
});
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit b77a2db

Please sign in to comment.