Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the hasTag method #519

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});