From eceb9a21a5f11f518d391fca30b347f8acce265b Mon Sep 17 00:00:00 2001 From: Mostafa El Bakly Date: Sun, 21 Aug 2016 14:20:56 +0200 Subject: [PATCH] Update Taggable.php adding existing in groups Added existingTagsInGroup Funtion Take array with groups name ["GroupA","GroupB"] and return Tags in those groups example Student::existingTagsInGroups(["basket-ball","foot-ball"]); result is collection of tags ["Playmaker","Defenders",etc..] --- src/Taggable.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Taggable.php b/src/Taggable.php index 2e0e862..95baf57 100644 --- a/src/Taggable.php +++ b/src/Taggable.php @@ -292,6 +292,23 @@ public static function existingTags() ->orderBy('tag_slug', 'ASC') ->get(array('tag_slug as slug', 'tag_name as name', 'tagging_tags.count as count')); } + + /** + * Return an array of all of the tags that are in use by this model + * @param $groups Array with groups names + * @return Collection + */ + public static function existingTagsInGroups(Array $groups) + { + return Tagged::distinct() + ->join('tagging_tags', 'tag_slug', '=', 'tagging_tags.slug') + ->join('tagging_tag_groups', 'tag_group_id', '=', 'tagging_tag_groups.id') + ->where('taggable_type', '=', (new static)->getMorphClass()) + ->whereIn('tagging_tag_groups.name',$groups) + ->orderBy('tag_slug', 'ASC') + ->get(array('tag_slug as slug', 'tag_name as name', 'tagging_tags.count as count')); + } + /** * Should untag on delete