Skip to content

Commit

Permalink
Add configurable taggable model class (#470)
Browse files Browse the repository at this point in the history
Co-authored-by: Benno Eggnauer <[email protected]>
  • Loading branch information
eggnaube and Benno Eggnauer authored Jul 20, 2023
1 parent 5a67bd4 commit 416cc5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
'taggable' => [
'table_name' => 'taggables',
'morph_name' => 'taggable',

/*
* The fully qualified class name of the pivot model.
*/
'class_name' => Illuminate\Database\Eloquent\Relations\MorphPivot::class,
]
];
7 changes: 7 additions & 0 deletions src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function getTaggableTableName(): string
return config('tags.taggable.table_name', 'taggables');
}

public function getPivotModelClassName(): string
{
return config('tags.taggable.class_name');
}

public static function bootHasTags()
{
static::created(function (Model $taggableModel) {
Expand All @@ -52,6 +57,7 @@ public function tags(): MorphToMany
{
return $this
->morphToMany(self::getTagClassName(), $this->getTaggableMorphName())
->using($this->getPivotModelClassName())
->ordered();
}

Expand All @@ -61,6 +67,7 @@ public function tagsTranslated(string | null $locale = null): MorphToMany

return $this
->morphToMany(self::getTagClassName(), $this->getTaggableMorphName())
->using($this->getPivotModelClassName())
->select('*')
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(name, '$.\"{$locale}\"')) as name_translated")
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(slug, '$.\"{$locale}\"')) as slug_translated")
Expand Down

0 comments on commit 416cc5b

Please sign in to comment.