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

Configable taggables morph table #458

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions config/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@
* The fully qualified class name of the tag model.
*/
'tag_model' => Spatie\Tags\Tag::class,

/*
* The name of the table associated with the taggable morph relation.
*/
'taggable' => [
'table_name' => 'taggables',
'morph_name' => 'taggable',
]
];
24 changes: 17 additions & 7 deletions src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public static function getTagClassName(): string
return config('tags.tag_model', Tag::class);
}

public static function getTaggableMorphName(): string
silnex marked this conversation as resolved.
Show resolved Hide resolved
{
return config('tags.taggable.morph_name', 'taggable');
}

public static function getTaggableTableName(): string
{
return config('tags.taggable.table_name', 'taggables');
}

public static function bootHasTags()
{
static::created(function (Model $taggableModel) {
Expand All @@ -41,16 +51,16 @@ public static function bootHasTags()
public function tags(): MorphToMany
{
return $this
->morphToMany(self::getTagClassName(), 'taggable')
->morphToMany(self::getTagClassName(), self::getTaggableMorphName())
->ordered();
}

public function tagsTranslated(string | null $locale = null): MorphToMany
{
$locale = ! is_null($locale) ? $locale : self::getTagClassName()::getLocale();
$locale = !is_null($locale) ? $locale : self::getTagClassName()::getLocale();

return $this
->morphToMany(self::getTagClassName(), 'taggable')
->morphToMany(self::getTagClassName(), self::getTaggableMorphName())
->select('*')
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(name, '$.\"{$locale}\"')) as name_translated")
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(slug, '$.\"{$locale}\"')) as slug_translated")
Expand All @@ -59,7 +69,7 @@ public function tagsTranslated(string | null $locale = null): MorphToMany

public function setTagsAttribute(string | array | ArrayAccess | Tag $tags)
{
if (! $this->exists) {
if (!$this->exists) {
$this->queuedTags = $tags;

return;
Expand Down Expand Up @@ -245,14 +255,14 @@ protected function syncTagIds($ids, string | null $type = null, $detaching = tru
// Get a list of tag_ids for all current tags
$current = $this->tags()
->newPivotStatement()
->where('taggable_id', $this->getKey())
->where('taggable_type', $this->getMorphClass())
->where(self::getTaggableMorphName() . '_id', $this->getKey())
->where(self::getTaggableMorphName() . '_type', $this->getMorphClass())
->when($type !== null, function ($query) use ($type) {
$tagModel = $this->tags()->getRelated();

return $query->join(
$tagModel->getTable(),
'taggables.tag_id',
self::getTaggableTableName() . '.tag_id',
'=',
$tagModel->getTable() . '.' . $tagModel->getKeyName()
)
Expand Down