Skip to content

Commit 36bb555

Browse files
committed
Doc Update
1 parent 911dcfb commit 36bb555

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ Migrate The database tables
2121
```bash
2222
php artisan migrate
2323
```
24+
It will migrate `tags` , `taggables` tables
2425

2526
#### Setup your models
27+
To creat a many to many polymorphic relation with your model and tags you need to use the `Taggify` trait.
2628
```php
2729
<?php
2830

@@ -40,7 +42,192 @@ class Post extends Model
4042
];
4143
}
4244
```
45+
#### Usage
46+
To attach/create new tags it uses addTags method. This method takes an array containing models or id or name of the tags you want to attach to your model and of course you can all use combination of these.
47+
48+
#### Attaching or Creating tags
49+
50+
```php
51+
<?php
52+
53+
$post = \App\Post::findOrFail(2);
54+
$post->addTags( [ 'Non Existing Tag', 'Spring Framework', 'Java']);
55+
56+
```
57+
Or
58+
```php
59+
<?php
60+
61+
$post = \App\Post::findOrFail(2);
62+
$tag = Tag::findOrFail(7);
63+
$post->addTags( [ $tag, 8, 9, 'Java']); // Tag model/ids/name
64+
65+
```
66+
The name will generate a unique slug for the tag and will increment the `count` column. The `count` column will represent how many times the tag's being used.
67+
68+
#### Detaching given tags
69+
70+
```php
71+
<?php
72+
73+
$post = \App\Post::findOrFail(2);
74+
$tag = Tag::findOrFail(7);
75+
$post->removeTags([ $tag, 8, 9, 'Java']);
76+
77+
```
78+
The remove tags will detach the given tags and decrement the `count` column.
79+
80+
#### Detaching all tags
4381

82+
```php
83+
<?php
84+
85+
$post = \App\Post::findOrFail(2);
86+
$post->removeAllTags();
87+
88+
```
89+
It will detach all the tags associated with the model and decrement the `count` column.
90+
91+
#### Re tag
92+
93+
```php
94+
<?php
95+
96+
$post = \App\Post::findOrFail(2);
97+
$post->reTag(['Python','PHP','php oop']);
98+
99+
```
100+
It will detach Previous Tags and attach Given Tags and decrement the `count` column.
101+
102+
#### Scopes
103+
104+
##### withAnyTag
105+
106+
```php
107+
<?php
108+
109+
$posts = Post::withAnyTag(['Laravel','Java', 9])->get()->dd();
110+
111+
```
112+
Gets The Models Associated with Any Given Tags.
113+
114+
115+
##### withAllTags
116+
117+
```php
118+
<?php
119+
120+
$posts = Post::withAllTags(['Laravel','Java', 9])->get()->dd();
121+
122+
```
123+
Finds The Models Where Given All Tags Are In Common.
124+
125+
##### popular
126+
127+
```php
128+
<?php
129+
130+
Tag::popular(5)->get()->dd(); // 5 is how many tags to display
131+
132+
```
133+
It will give the most popular tags based on `count` column.
134+
135+
##### unPopular
136+
137+
```php
138+
<?php
139+
140+
Tag::unPopular(5)->get()->dd(); // 5 is how many tags to display
141+
142+
```
143+
Gets Less Used Tags based on `count` column.
144+
145+
##### unUsed
146+
147+
```php
148+
<?php
149+
150+
Tag::unUsed(5)->get()->dd(); // 5 is how many tags to display
151+
152+
```
153+
Gets Unused Tags based on `count` column.
154+
155+
##### usedMoreThan
156+
157+
```php
158+
<?php
159+
160+
Tag::usedMoreThan(5)->take(5)->get()->dd(); ; // more then 5 times tag's being used
161+
162+
```
163+
Return tags that are used more than given times based on `count` column.
164+
165+
##### usedLessThan
166+
167+
```php
168+
<?php
169+
170+
Tag::usedLessThan(5)->take(5)->get()->dd(); // not more then 5 times tag's being used
171+
172+
```
173+
Return tags that are used less than given times based on `count` column.
174+
175+
#### Helpers
176+
The package comes with 2 useful helpers, which you can use throughout your application.
177+
178+
##### popular_tags_by_model
179+
180+
```blade
181+
<ul>
182+
@foreach (popular_tags_by_model("App\Post") as $popular_tag)
183+
<!-- Model class, number of items to take -->
184+
<li>
185+
<p>{{$popular_tag->tag_id}}</p>
186+
<p>{{$popular_tag->tag_name}}</p>
187+
<p>{{$popular_tag->tag_slug}}</p>
188+
<p>{{$popular_tag->model_count}}</p>
189+
<p>{{$popular_tag->tag_description}}</p>
190+
</li>
191+
@endforeach
192+
</ul>
193+
194+
```
195+
Gets Popular Tags In A Specific Model.
196+
197+
##### popular_tags
198+
199+
```blade
200+
<ul>
201+
@foreach (popular_tags(5) as $popular_tag)
202+
<!-- number of items to take -->
203+
<li>
204+
<p>{{$popular_tag->id}}</p>
205+
<p>{{$popular_tag->slug}}</p>
206+
<p>{{$popular_tag->name}}</p>
207+
<p>{{$popular_tag->count}}</p>
208+
<p>{{$popular_tag->description}}</p>
209+
</li>
210+
@endforeach
211+
</ul>
212+
213+
```
214+
Gets Popular Tags In All Models
215+
216+
#### Inverse Relation in Tag Model
217+
218+
use items() method and give a model
219+
220+
```php
221+
<?php
222+
223+
$tag = Tag::findOrFail(7);
224+
$tag = $tag->items(Post::class)->take(5)->get()->dd();
225+
226+
```
227+
Inverse Polymorphic Between Tag and A Model.
228+
229+
**You can also add inverse relationship by extending the `AmitKD\LaravelTaggify\Models\Tag` model**
230+
44231
#### Credits
45232

46233
- Amit Kollol Dey - http://amitkolloldey.me

0 commit comments

Comments
 (0)