Skip to content

Commit 416cc5b

Browse files
eggnaubeBenno Eggnauer
and
Benno Eggnauer
authored
Add configurable taggable model class (spatie#470)
Co-authored-by: Benno Eggnauer <[email protected]>
1 parent 5a67bd4 commit 416cc5b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

config/tags.php

+5
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
'taggable' => [
2020
'table_name' => 'taggables',
2121
'morph_name' => 'taggable',
22+
23+
/*
24+
* The fully qualified class name of the pivot model.
25+
*/
26+
'class_name' => Illuminate\Database\Eloquent\Relations\MorphPivot::class,
2227
]
2328
];

src/HasTags.php

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function getTaggableTableName(): string
2929
return config('tags.taggable.table_name', 'taggables');
3030
}
3131

32+
public function getPivotModelClassName(): string
33+
{
34+
return config('tags.taggable.class_name');
35+
}
36+
3237
public static function bootHasTags()
3338
{
3439
static::created(function (Model $taggableModel) {
@@ -52,6 +57,7 @@ public function tags(): MorphToMany
5257
{
5358
return $this
5459
->morphToMany(self::getTagClassName(), $this->getTaggableMorphName())
60+
->using($this->getPivotModelClassName())
5561
->ordered();
5662
}
5763

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

6268
return $this
6369
->morphToMany(self::getTagClassName(), $this->getTaggableMorphName())
70+
->using($this->getPivotModelClassName())
6471
->select('*')
6572
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(name, '$.\"{$locale}\"')) as name_translated")
6673
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(slug, '$.\"{$locale}\"')) as slug_translated")

0 commit comments

Comments
 (0)