From c66934287585ea4f97e29da6b1204b7ae60bd1f4 Mon Sep 17 00:00:00 2001 From: Max Schwanekamp Date: Mon, 1 Jan 2024 18:43:56 -0800 Subject: [PATCH 1/2] Fix #140 - Use getMorphClass() instead of get_class to resolve the taggable_type. Add same to the anonymous class in getQualifiedPivotTableName(). --- src/Services/TagService.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 489d881..f4d1187 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -218,7 +218,7 @@ public function getAllTags($class = null): Collection LEFT JOIN {$tagTable} t ON tt.tag_id=t.tag_id WHERE tt.taggable_type = ?"; - return $this->tagModel::fromQuery($sql, [$class]); + return $this->tagModel::fromQuery($sql, [$this->getClassTaggableType($class)]); } /** @@ -288,7 +288,7 @@ public function getPopularTags(int $limit = null, $class = null, int $minCount = if ($class) { $sql .= ' WHERE tt.taggable_type = ?'; - $bindings[] = ($class instanceof Model) ? get_class($class) : $class; + $bindings[] = $this->getClassTaggableType($class); } // group by everything to handle strict and non-strict mode in MySQL @@ -383,10 +383,23 @@ private function getQualifiedTagTableName(): string private function getQualifiedPivotTableName(string $class=null): string { /** @var \Cviebrock\EloquentTaggable\Taggable $instance */ - $instance = $class ? new $class : new class extends Model { use Taggable; }; + $instance = $class + ? new $class + : new class extends Model { + use Taggable; + function getMorphClass() { + return 'taggable-anonymous'; // any value will work, to keep Relation::enforceMorphMap() happy + } + }; return $instance->tags()->getConnection()->getTablePrefix() . $instance->tags()->getTable(); } + private function getClassTaggableType($class): string + { + return $class instanceof Model + ? $class->getMorphClass() + : (new $class)->getMorphClass(); + } } From 182f00025f0188ef17501ed85a83de4ea55b57dd Mon Sep 17 00:00:00 2001 From: Max Schwanekamp Date: Mon, 1 Jan 2024 18:54:42 -0800 Subject: [PATCH 2/2] Use Pivot class as default in anonymous class, since that's what HasRelations does for a pivot anyway. --- src/Services/TagService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Services/TagService.php b/src/Services/TagService.php index f4d1187..b96a7e0 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphToMany; +use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Support\Collection as BaseCollection; @@ -388,7 +389,7 @@ private function getQualifiedPivotTableName(string $class=null): string : new class extends Model { use Taggable; function getMorphClass() { - return 'taggable-anonymous'; // any value will work, to keep Relation::enforceMorphMap() happy + return Pivot::class; } };