Skip to content

Commit

Permalink
Merge pull request #141 from celearning/master
Browse files Browse the repository at this point in the history
Fixes #140 ... thanks @anaxamaxan
  • Loading branch information
cviebrock authored Jan 6, 2024
2 parents 92cdbf5 + 182f000 commit 55ed8c9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Services/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -218,7 +219,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)]);
}

/**
Expand Down Expand Up @@ -288,7 +289,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
Expand Down Expand Up @@ -383,10 +384,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 Pivot::class;
}
};

return $instance->tags()->getConnection()->getTablePrefix() .
$instance->tags()->getTable();
}

private function getClassTaggableType($class): string
{
return $class instanceof Model
? $class->getMorphClass()
: (new $class)->getMorphClass();
}
}

0 comments on commit 55ed8c9

Please sign in to comment.