Skip to content

Commit

Permalink
Merge pull request #124 from Woutrrr/laravel-5
Browse files Browse the repository at this point in the history
implement withoutTags scope from issue #115
  • Loading branch information
rtconner authored Apr 28, 2017
2 parents 219df35 + 3b456f4 commit 7932f38
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Article::withAnyTag(['Gardening','Cooking'])->get(); // fetch articles with any

Article::withAllTags(['Gardening', 'Cooking'])->get(); // only fetch articles with all the tags

Article::withoutTags(['Gardening', 'Cooking'])->get(); // only fetch articles without all tags listed

Conner\Tagging\Model\Tag::where('count', '>', 2)->get(); // return all tags used more than twice

Article::existingTags(); // return collection of all existing tags on any articles
Expand Down
28 changes: 28 additions & 0 deletions src/Taggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ public function scopeWithAnyTag($query, $tagNames)
$primaryKey = $this->getKeyName();
return $query->whereIn($this->getTable().'.'.$primaryKey, $tags);
}

/**
* Filter model to subset without the given tags
*
* @param $tagNames array|string
*/
public function scopeWithoutTags($query, $tagNames)
{
if(!is_array($tagNames)) {
$tagNames = func_get_args();
array_shift($tagNames);
}

$tagNames = static::$taggingUtility->makeTagArray($tagNames);

$normalizer = config('tagging.normalizer');
$normalizer = $normalizer ?: [static::$taggingUtility, 'slug'];

$tagNames = array_map($normalizer, $tagNames);
$className = $query->getModel()->getMorphClass();

$tags = Tagged::whereIn('tag_slug', $tagNames)
->where('taggable_type', $className)
->get()->pluck('taggable_id');

$primaryKey = $this->getKeyName();
return $query->whereNotIn($this->getTable().'.'.$primaryKey, $tags);
}

/**
* Adds a single tag
Expand Down

0 comments on commit 7932f38

Please sign in to comment.