Skip to content

Commit b77a2db

Browse files
author
FreedomKnight
committed
fix morphs
1 parent ab94965 commit b77a2db

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Models/Category.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function boot()
2323

2424
public function categorizables($class)
2525
{
26-
return $this->morphedByMany($class, 'categorizable');
26+
return $this->morphedByMany($class, 'categorizable', 'categorizable', 'category_id');
2727
}
2828

2929
public function getRelationValue($key)
@@ -34,7 +34,7 @@ public function getRelationValue($key)
3434

3535
if (array_key_exists($key, config('categorizable.morphs', []))) {
3636
$class = config('categorizable.morphs')[$key];
37-
$relation = $this->morphedByMany($class, 'categorizable', 'categorizable', 'id');
37+
$relation = $this->categorizables($class);
3838
return tap($relation->getResults(), function ($results) use ($key) {
3939
$this->setRelation($key, $results);
4040
});
@@ -48,7 +48,7 @@ public function __call($method, $arguments)
4848
{
4949
if (array_key_exists($method, config('categorizable.morphs', []))) {
5050
$class = config('categorizable.morphs')[$method];
51-
return $this->morphedByMany($class, 'categorizable', 'categorizable', 'id');
51+
return $this->categorizables($class);
5252
}
5353

5454
return parent::__call($method, $arguments);

tests/CategoryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,16 @@ public function testFindModelHasCategoriesByUnexistCategory()
193193
{
194194
$this->assertCount(0, TestModel::hasCategories('Nothing')->get());
195195
}
196+
197+
public function testListMorphs()
198+
{
199+
config()->set('categorizable.morphs', ['tests' => TestModel::class]);
200+
$category = Category::create(['name' => 'news']);
201+
$foo = TestModel::create(['title' => 'foo']);
202+
$bar = TestModel::create(['title' => 'bar']);
203+
$foo->categorize($category->id);
204+
$bar->categorize($category->id);
205+
$category->refresh();
206+
$this->assertCount(2, $category->load('tests')->tests);
207+
}
196208
}

0 commit comments

Comments
 (0)