Skip to content

Commit e9d5d97

Browse files
committed
Fixes #196
1 parent 6eea6ac commit e9d5d97

5 files changed

Lines changed: 22 additions & 9 deletions

File tree

docs/tag-groups.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ TagGroups are a simple feature that let you group a set of tags together
77
php artisan tagging:create-group MyTagGroup
88
```
99

10-
Set the tag group for a tag
10+
Assign a group when tagging a model
11+
12+
```php
13+
$article->tag('Gardening', group: 'MyTagGroup');
14+
$article->tag(['Gardening', 'Floral'], group: 'MyTagGroup');
15+
```
16+
17+
Or set the tag group directly on a tag
1118

1219
```php
1320
$tag->setGroup('MyTagGroup');

docs/usage-examples.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ foreach($article->tags as $tag) {
1515
$article->tag('Gardening'); // attach the tag
1616
$article->tag('Gardening, Floral'); // attach the tag
1717
$article->tag(['Gardening', 'Floral']); // attach the tag
18+
$article->tag('Gardening', group: 'MyTagGroup'); // attach the tag and assign to a group
19+
$article->tag('Gardening', locale: 'fr'); // attach the tag with a locale
1820

1921
$article->untag('Cooking'); // remove Cooking tag
2022
$article->untag(); // remove all tags

src/Contracts/TaggableContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface TaggableContract
1414
*
1515
* @param string|array $tagNames
1616
*/
17-
public function addTags($tagNames, $locale = null);
17+
public function addTags($tagNames, $locale = null, $group = null);
1818

1919
/**
2020
* Return array of the tag names related to the current model

src/Taggable.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ public function getTagNamesAttribute(): array
9797
* @param string|array $tagNames
9898
* @return void
9999
*/
100-
public function addTags($tagNames, $locale = null)
100+
public function addTags($tagNames, $locale = null, $group = null)
101101
{
102102
$tagNames = TaggingUtility::makeTagArray($tagNames);
103103

104104
foreach ($tagNames as $tagName) {
105-
$this->addSingleTag($tagName, $locale);
105+
$this->addSingleTag($tagName, $locale, $group);
106106
}
107107
}
108108

@@ -112,9 +112,9 @@ public function addTags($tagNames, $locale = null)
112112
* @param string|array $tagNames
113113
* @return void
114114
*/
115-
public function tag($tagNames, $locale = null)
115+
public function tag($tagNames, $locale = null, $group = null)
116116
{
117-
$this->addTags($tagNames, $locale);
117+
$this->addTags($tagNames, $locale, $group);
118118
}
119119

120120
/**
@@ -242,7 +242,7 @@ public function scopeWithoutTags(Builder $query, $tagNames): Builder
242242
*
243243
* @param string $tagName
244244
*/
245-
private function addSingleTag($tagName, $locale = null)
245+
private function addSingleTag($tagName, $locale = null, $group = null)
246246
{
247247
$tagName = trim($tagName);
248248

@@ -266,7 +266,7 @@ private function addSingleTag($tagName, $locale = null)
266266

267267
$this->tagged()->save($tagged);
268268

269-
TaggingUtility::incrementCount($tagName, $tagSlug, 1, $locale);
269+
TaggingUtility::incrementCount($tagName, $tagSlug, 1, $locale, $group);
270270

271271
unset($this->relations['tagged']);
272272

src/TaggingUtility.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public static function slug($str)
200200
* @param string $tagSlug
201201
* @param int $count
202202
*/
203-
public static function incrementCount($tagString, $tagSlug, $count, $locale = null)
203+
public static function incrementCount($tagString, $tagSlug, $count, $locale = null, $group = null)
204204
{
205205
if ($count <= 0) {
206206
return;
@@ -225,6 +225,10 @@ public static function incrementCount($tagString, $tagSlug, $count, $locale = nu
225225

226226
$tag->count = $tag->count + $count;
227227
$tag->save();
228+
229+
if ($group) {
230+
$tag->setGroup($group);
231+
}
228232
}
229233

230234
/**

0 commit comments

Comments
 (0)