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

simplified way to get tags from associated table #474

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 20 additions & 5 deletions src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;

trait HasTags
Expand All @@ -19,6 +20,20 @@ public static function getTagClassName(): string
return config('tags.tag_model', Tag::class);
}

protected function getTagsThroughClass() {
if (isset(static::$taggedModel)) {
// This has to be the least effective way to do this...
$model = static::$taggedModel;
$instance = new $model;
$source = Str::singular(class_basename($instance)) . '.tags';
}
else {
$source = "tags";
}

return $source;
}

public function getTaggableMorphName(): string
{
return config('tags.taggable.morph_name', 'taggable');
Expand Down Expand Up @@ -93,7 +108,7 @@ public function scopeWithAllTags(
$tags = static::convertToTags($tags, $type);

collect($tags)->each(function ($tag) use ($query) {
$query->whereHas('tags', function (Builder $query) use ($tag) {
$query->whereHas($this->getTagsThroughClass(), function (Builder $query) use ($tag) {
$query->where('tags.id', $tag->id ?? 0);
});
});
Expand All @@ -109,7 +124,7 @@ public function scopeWithAnyTags(
$tags = static::convertToTags($tags, $type);

return $query
->whereHas('tags', function (Builder $query) use ($tags) {
->whereHas($this->getTagsThroughClass(), function (Builder $query) use ($tags) {
$tagIds = collect($tags)->pluck('id');

$query->whereIn('tags.id', $tagIds);
Expand All @@ -124,7 +139,7 @@ public function scopeWithoutTags(
$tags = static::convertToTags($tags, $type);

return $query
->whereDoesntHave('tags', function (Builder $query) use ($tags) {
->whereDoesntHave($this->getTagsThroughClass(), function (Builder $query) use ($tags) {
$tagIds = collect($tags)->pluck('id');

$query->whereIn('tags.id', $tagIds);
Expand All @@ -138,7 +153,7 @@ public function scopeWithAllTagsOfAnyType(Builder $query, $tags): Builder
collect($tags)
->each(function ($tag) use ($query) {
$query->whereHas(
'tags',
$this->getTagsThroughClass(),
fn (Builder $query) => $query->where('tags.id', $tag ? $tag->id : 0)
);
});
Expand All @@ -153,7 +168,7 @@ public function scopeWithAnyTagsOfAnyType(Builder $query, $tags): Builder
$tagIds = collect($tags)->pluck('id');

return $query->whereHas(
'tags',
$this->getTagsThroughClass(),
fn (Builder $query) => $query->whereIn('tags.id', $tagIds)
);
}
Expand Down