Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #140 #141

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading