Skip to content

Commit 73ef478

Browse files
gtmasseyfreekmurze
andauthored
Allow custom tag table name and primary key for scoped queries (#529)
* Added customizable tag table and primary key through config file * updated default config file * removed redundant config options * Update HasTags.php --------- Co-authored-by: Freek Van der Herten <[email protected]>
1 parent c0ac810 commit 73ef478

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/HasTags.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ public static function getTagClassName(): string
2020
return config('tags.tag_model', Tag::class);
2121
}
2222

23+
public static function getTagTableName(): string
24+
{
25+
$tagInstance = new (self::getTagClassName());
26+
27+
return $tagInstance->getTable();
28+
}
29+
30+
public static function getTagTablePrimaryKeyName(): string
31+
{
32+
return self::getTagTableName() . '.' . self::getTagPrimaryKey();
33+
}
34+
35+
public static function getTagPrimaryKey(): string
36+
{
37+
$tagInstance = new (self::getTagClassName());
38+
return $tagInstance->getKeyName();
39+
}
40+
2341
public function getTaggableMorphName(): string
2442
{
2543
return config('tags.taggable.morph_name', 'taggable');
@@ -95,7 +113,7 @@ public function scopeWithAllTags(
95113

96114
collect($tags)->each(function ($tag) use ($query) {
97115
$query->whereHas('tags', function (Builder $query) use ($tag) {
98-
$query->where('tags.id', $tag->id ?? 0);
116+
$query->where(self::getTagTablePrimaryKeyName(), $tag->id ?? 0);
99117
});
100118
});
101119

@@ -113,7 +131,7 @@ public function scopeWithAnyTags(
113131
->whereHas('tags', function (Builder $query) use ($tags) {
114132
$tagIds = collect($tags)->pluck('id');
115133

116-
$query->whereIn('tags.id', $tagIds);
134+
$query->whereIn(self::getTagTablePrimaryKeyName(), $tagIds);
117135
});
118136
}
119137

@@ -128,7 +146,7 @@ public function scopeWithoutTags(
128146
->whereDoesntHave('tags', function (Builder $query) use ($tags) {
129147
$tagIds = collect($tags)->pluck('id');
130148

131-
$query->whereIn('tags.id', $tagIds);
149+
$query->whereIn(self::getTagTablePrimaryKeyName(), $tagIds);
132150
});
133151
}
134152

@@ -140,7 +158,7 @@ public function scopeWithAllTagsOfAnyType(Builder $query, $tags): Builder
140158
->each(function ($tag) use ($query) {
141159
$query->whereHas(
142160
'tags',
143-
fn (Builder $query) => $query->where('tags.id', $tag ? $tag->id : 0)
161+
fn (Builder $query) => $query->where(self::getTagTablePrimaryKeyName(), $tag ? $tag->id : 0)
144162
);
145163
});
146164

@@ -155,7 +173,7 @@ public function scopeWithAnyTagsOfAnyType(Builder $query, $tags): Builder
155173

156174
return $query->whereHas(
157175
'tags',
158-
fn (Builder $query) => $query->whereIn('tags.id', $tagIds)
176+
fn (Builder $query) => $query->whereIn(self::getTagTablePrimaryKeyName(), $tagIds)
159177
);
160178
}
161179

0 commit comments

Comments
 (0)