Skip to content

Commit ffdeb06

Browse files
committed
Added a console command for created tag groups.
Added helper function for setting the group on a tag Updated readme
1 parent 66ec898 commit ffdeb06

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ Article::existingTags(); // return collection of all existing tags on any articl
8484

8585
[More examples in the documentation](docs/usage-examples.md)
8686

87+
88+
### Tag Groups
89+
90+
You can create groups with the following artisan command
91+
92+
```php
93+
php artisan tagging:create-group MyTagGroup
94+
```
95+
96+
Set the tag group for a tag
97+
98+
```php
99+
$tag->setGroup('MyTagGroup');
100+
```
101+
102+
To get all the tags in a certain group
103+
104+
```php
105+
Tag::inGroup('MyTagGroup')->get()
106+
```
107+
108+
Check if a tag is in a group
109+
110+
```php
111+
$tag->isInGroup('MyTagGroup');
112+
```
113+
114+
87115
### Configure
88116

89117
[See config/tagging.php](config/tagging.php) for configuration options.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Conner\Tagging\Console\Commands;
4+
5+
use Conner\Tagging\Contracts\TaggingUtility;
6+
use Conner\Tagging\Model\TagGroup;
7+
use Illuminate\Console\Command;
8+
9+
10+
class GenerateTagGroup extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'tagging:create-group {group_name}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Create a laravel tag group';
25+
26+
27+
protected $taggingUtility;
28+
29+
30+
31+
public function __construct(TaggingUtility $taggingUtility)
32+
{
33+
parent::__construct();
34+
35+
$this->taggingUtility = $taggingUtility;
36+
}
37+
38+
39+
40+
/**
41+
* Execute the console command.
42+
*
43+
* @return mixed
44+
*/
45+
public function handle()
46+
{
47+
$group_name = $this->argument('group_name');
48+
49+
$tag_group = new TagGroup();
50+
$tag_group->name = $group_name;
51+
$tag_group->slug = $this->taggingUtility->slug($group_name);
52+
53+
$tag_group->save();
54+
55+
56+
$this->info('Created tag group: ' . $group_name);
57+
}
58+
}

src/Model/Tag.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ public function save(array $options = array())
5050
}
5151
}
5252

53+
/**
54+
* Tag group setter
55+
*/
56+
public function setGroup($group_name)
57+
{
58+
$tagGroup = TagGroup::where('slug', $this->taggingUtility->slug($group_name))->first();
59+
60+
if($tagGroup){
61+
$this->group()->associate($tagGroup);
62+
$this->save();
63+
64+
return $this;
65+
}else{
66+
throw new \Exception('No Tag Group found');
67+
}
68+
69+
}
70+
71+
5372

5473
/**
5574
* Tag group helper function

src/Providers/TaggingServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
*/
1212
class TaggingServiceProvider extends ServiceProvider
1313
{
14+
15+
protected $commands = [
16+
\Conner\Tagging\Console\Commands\GenerateTagGroup::class
17+
];
18+
19+
1420
/**
1521
* Bootstrap the application events.
1622
*/
@@ -32,6 +38,9 @@ public function boot()
3238
*/
3339
public function register()
3440
{
41+
42+
$this->commands($this->commands);
43+
3544
$this->app->singleton(TaggingUtility::class, function () {
3645
return new Util;
3746
});

0 commit comments

Comments
 (0)