Skip to content

Commit

Permalink
Document the hasTag method in the README.md and `docs/basic-usage…
Browse files Browse the repository at this point in the history
…/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
  • Loading branch information
AlexVanderbist committed Oct 3, 2024
1 parent 7f7bc48 commit d1384d2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ $tag2->order_column; //returns 2

// manipulating the order of tags
$tag->swapOrder($anotherTag);

// checking if a model has a tag
$newsItem->hasTag('first tag');
$newsItem->hasTag('first tag', 'some_type');
```

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).
Expand Down
16 changes: 16 additions & 0 deletions docs/basic-usage/using-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,19 @@ You can fetch a collection of all registered tag types by using the static metho
```php
Tag::getTypes();
```

## Checking if a model has a tag

You can check if a model has a specific tag using the `hasTag` method:

```php
$yourModel->hasTag('tag 1'); // returns true if the model has the tag
$yourModel->hasTag('non-existing tag'); // returns false if the model does not have the tag
```

You can also check if a model has a tag with a specific type:

```php
$yourModel->hasTag('tag 1', 'some_type'); // returns true if the model has the tag with the specified type
$yourModel->hasTag('tag 1', 'non-existing type'); // returns false if the model does not have the tag with the specified type
```
9 changes: 9 additions & 0 deletions src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,13 @@ protected function syncTagIds($ids, string | null $type = null, $detaching = tru
$this->tags()->touchIfTouching();
}
}

public function hasTag($tag, string $type = null): bool
{
return $this->tags
->when($type !== null, fn ($query) => $query->where('type', $type))
->contains(function ($modelTag) use ($tag) {
return $modelTag->name === $tag || $modelTag->id === $tag;
});
}
}
22 changes: 22 additions & 0 deletions tests/HasTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,25 @@
$tagsOfTypeA = $this->testModel->tagsWithType('typeA');
expect($tagsOfTypeA->pluck('name')->toArray())->toEqual(['tagA1']);
});

it('can check if it has a tag', function () {
$tag = Tag::findOrCreate('test-tag');
$anotherTag = Tag::findOrCreate('another-tag');

$this->testModel->attachTag($tag);

expect($this->testModel->hasTag('test-tag'))->toBeTrue();
expect($this->testModel->hasTag($tag->id))->toBeTrue();
expect($this->testModel->hasTag('non-existing-tag'))->toBeFalse();
expect($this->testModel->hasTag($anotherTag->id))->toBeFalse();
});

it('can check if it has a tag with type', function () {
$tag = Tag::findOrCreate('test-tag', 'type1');
$sameNameDifferentType = Tag::findOrCreate('test-tag', 'type2');

$this->testModel->attachTag($tag);

expect($this->testModel->hasTag('test-tag', 'type1'))->toBeTrue();
expect($this->testModel->hasTag('test-tag', 'type2'))->toBeFalse();
});

0 comments on commit d1384d2

Please sign in to comment.