Skip to content

Commit d1384d2

Browse files
Document the hasTag method in the README.md and docs/basic-usage/using-tags.md
* **README.md** - Add a section to document the `hasTag` method - Include examples of how to use the `hasTag` method * **docs/basic-usage/using-tags.md** - Add a section to document the `hasTag` method - Include examples of how to use the `hasTag` method
1 parent 7f7bc48 commit d1384d2

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ $tag2->order_column; //returns 2
9898

9999
// manipulating the order of tags
100100
$tag->swapOrder($anotherTag);
101+
102+
// checking if a model has a tag
103+
$newsItem->hasTag('first tag');
104+
$newsItem->hasTag('first tag', 'some_type');
101105
```
102106

103107
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

docs/basic-usage/using-tags.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,19 @@ You can fetch a collection of all registered tag types by using the static metho
9999
```php
100100
Tag::getTypes();
101101
```
102+
103+
## Checking if a model has a tag
104+
105+
You can check if a model has a specific tag using the `hasTag` method:
106+
107+
```php
108+
$yourModel->hasTag('tag 1'); // returns true if the model has the tag
109+
$yourModel->hasTag('non-existing tag'); // returns false if the model does not have the tag
110+
```
111+
112+
You can also check if a model has a tag with a specific type:
113+
114+
```php
115+
$yourModel->hasTag('tag 1', 'some_type'); // returns true if the model has the tag with the specified type
116+
$yourModel->hasTag('tag 1', 'non-existing type'); // returns false if the model does not have the tag with the specified type
117+
```

src/HasTags.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,13 @@ protected function syncTagIds($ids, string | null $type = null, $detaching = tru
302302
$this->tags()->touchIfTouching();
303303
}
304304
}
305+
306+
public function hasTag($tag, string $type = null): bool
307+
{
308+
return $this->tags
309+
->when($type !== null, fn ($query) => $query->where('type', $type))
310+
->contains(function ($modelTag) use ($tag) {
311+
return $modelTag->name === $tag || $modelTag->id === $tag;
312+
});
313+
}
305314
}

tests/HasTagsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,25 @@
349349
$tagsOfTypeA = $this->testModel->tagsWithType('typeA');
350350
expect($tagsOfTypeA->pluck('name')->toArray())->toEqual(['tagA1']);
351351
});
352+
353+
it('can check if it has a tag', function () {
354+
$tag = Tag::findOrCreate('test-tag');
355+
$anotherTag = Tag::findOrCreate('another-tag');
356+
357+
$this->testModel->attachTag($tag);
358+
359+
expect($this->testModel->hasTag('test-tag'))->toBeTrue();
360+
expect($this->testModel->hasTag($tag->id))->toBeTrue();
361+
expect($this->testModel->hasTag('non-existing-tag'))->toBeFalse();
362+
expect($this->testModel->hasTag($anotherTag->id))->toBeFalse();
363+
});
364+
365+
it('can check if it has a tag with type', function () {
366+
$tag = Tag::findOrCreate('test-tag', 'type1');
367+
$sameNameDifferentType = Tag::findOrCreate('test-tag', 'type2');
368+
369+
$this->testModel->attachTag($tag);
370+
371+
expect($this->testModel->hasTag('test-tag', 'type1'))->toBeTrue();
372+
expect($this->testModel->hasTag('test-tag', 'type2'))->toBeFalse();
373+
});

0 commit comments

Comments
 (0)