Skip to content

Commit

Permalink
Fix find from string of any type (#375)
Browse files Browse the repository at this point in the history
* add find from string of any type to docs

* Update find from string of any type to return collection and update tests
  • Loading branch information
delta1186 authored Nov 24, 2021
1 parent 7b7e360 commit 78997ee
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
7 changes: 5 additions & 2 deletions docs/basic-usage/using-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use Spatie\Tags\HasTags;
class YourModel extends Model
{
use HasTags;

...
}
```
Expand Down Expand Up @@ -70,13 +70,16 @@ $tag->name = 'another tag';
$tag->save();

//use "findFromString" instead of "find" to retrieve a certain tag
$tag = Tag::findFromString('another tag')
$tag = Tag::findFromString('another tag');

//create a tag if it doesn't exist yet
$tag = Tag::findOrCreateFromString('yet another tag');

//delete a tag
$tag->delete();

//use "findFromStringOfAnyType" to retrieve a collection of tags with various types
$tags = Tag::findFromStringOfAnyType('one more tag');
```

## Finding tags
Expand Down
3 changes: 1 addition & 2 deletions src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public function scopeWithAnyTagsOfAnyType(Builder $query, $tags): Builder

$tagIds = collect($tags)->pluck('id');


return $query->whereHas(
'tags',
fn (Builder $query) => $query->whereIn('tags.id', $tagIds)
Expand Down Expand Up @@ -216,7 +215,7 @@ protected static function convertToTagsOfAnyType($values, $locale = null)
$className = static::getTagClassName();

return $className::findFromStringOfAnyType($value, $locale);
});
})->flatten();
}

protected function syncTagIds($ids, string | null $type = null, $detaching = true): void
Expand Down
2 changes: 1 addition & 1 deletion src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function findFromStringOfAnyType(string $name, string $locale = nu

return static::query()
->where("name->{$locale}", $name)
->first();
->get();
}

protected static function findOrCreateFromString(string $name, string $type = null, string $locale = null)
Expand Down
24 changes: 22 additions & 2 deletions tests/HasTagsScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,38 @@ public function it_provides_as_scope_to_get_all_models_that_have_all_of_the_give
}

/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_any_of_the_given_tags_with_any_type()
public function it_provides_a_scope_to_get_all_models_that_have_any_of_the_given_tags_with_any_type()
{
$testModels = TestModel::withAnyTagsOfAnyType(['tagE', 'tagF'])->get();

$this->assertEquals(['model5', 'model6'], $testModels->pluck('name')->toArray());
}

/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_all_of_the_given_tags_with_any_type()
public function it_provides_a_scope_to_get_all_models_that_have_any_of_the_given_tags_with_any_type_from_mixed_tag_values()
{
$tagD = Tag::findFromString('tagD');

$testModels = TestModel::withAnyTagsOfAnyType([$tagD, 'tagE', 'tagF'])->get();

$this->assertEquals(['model4', 'model5', 'model6'], $testModels->pluck('name')->toArray());
}

/** @test */
public function it_provides_a_scope_to_get_all_models_that_have_all_of_the_given_tags_with_any_type()
{
$testModels = TestModel::withAllTagsOfAnyType(['tagE', 'tagF'])->get();

$this->assertEquals(['model5'], $testModels->pluck('name')->toArray());
}

/** @test */
public function it_provides_a_scope_to_get_all_models_that_have_all_of_the_given_tags_with_any_type_from_mixed_tag_values()
{
$tagE = Tag::findFromString('tagE', 'typedTag');

$testModels = TestModel::withAllTagsOfAnyType([$tagE, 'tagF'])->get();

$this->assertEquals(['model5'], $testModels->pluck('name')->toArray());
}
}
14 changes: 14 additions & 0 deletions tests/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ public function it_can_find_or_create_a_tag()
$this->assertEquals('string', $tag2->name);
}

/** @test */
public function it_can_find_tags_from_a_string_with_any_type()
{
Tag::findOrCreate('tag1');

Tag::findOrCreate('tag1', 'myType1');

Tag::findOrCreate('tag1', 'myType2');

$tags = Tag::findFromStringOfAnyType('tag1');

$this->assertCount(3, $tags);
}

/** @test */
public function its_name_can_be_changed_by_setting_its_name_property_to_a_new_value()
{
Expand Down

0 comments on commit 78997ee

Please sign in to comment.