Skip to content

Commit 7932f38

Browse files
authored
Merge pull request #124 from Woutrrr/laravel-5
implement withoutTags scope from issue #115
2 parents 219df35 + 3b456f4 commit 7932f38

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Article::withAnyTag(['Gardening','Cooking'])->get(); // fetch articles with any
7777

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

80+
Article::withoutTags(['Gardening', 'Cooking'])->get(); // only fetch articles without all tags listed
81+
8082
Conner\Tagging\Model\Tag::where('count', '>', 2)->get(); // return all tags used more than twice
8183

8284
Article::existingTags(); // return collection of all existing tags on any articles

src/Taggable.php

+28
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,34 @@ public function scopeWithAnyTag($query, $tagNames)
223223
$primaryKey = $this->getKeyName();
224224
return $query->whereIn($this->getTable().'.'.$primaryKey, $tags);
225225
}
226+
227+
/**
228+
* Filter model to subset without the given tags
229+
*
230+
* @param $tagNames array|string
231+
*/
232+
public function scopeWithoutTags($query, $tagNames)
233+
{
234+
if(!is_array($tagNames)) {
235+
$tagNames = func_get_args();
236+
array_shift($tagNames);
237+
}
238+
239+
$tagNames = static::$taggingUtility->makeTagArray($tagNames);
240+
241+
$normalizer = config('tagging.normalizer');
242+
$normalizer = $normalizer ?: [static::$taggingUtility, 'slug'];
243+
244+
$tagNames = array_map($normalizer, $tagNames);
245+
$className = $query->getModel()->getMorphClass();
246+
247+
$tags = Tagged::whereIn('tag_slug', $tagNames)
248+
->where('taggable_type', $className)
249+
->get()->pluck('taggable_id');
250+
251+
$primaryKey = $this->getKeyName();
252+
return $query->whereNotIn($this->getTable().'.'.$primaryKey, $tags);
253+
}
226254

227255
/**
228256
* Adds a single tag

0 commit comments

Comments
 (0)