From 78997eeddd00f30a186d1fcbc1c12c200baa59bc Mon Sep 17 00:00:00 2001 From: Kevin Hicks Date: Wed, 24 Nov 2021 03:13:20 -0600 Subject: [PATCH] Fix find from string of any type (#375) * add find from string of any type to docs * Update find from string of any type to return collection and update tests --- docs/basic-usage/using-tags.md | 7 +++++-- src/HasTags.php | 3 +-- src/Tag.php | 2 +- tests/HasTagsScopesTest.php | 24 ++++++++++++++++++++++-- tests/TagTest.php | 14 ++++++++++++++ 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/docs/basic-usage/using-tags.md b/docs/basic-usage/using-tags.md index 051f30c..fd9d247 100644 --- a/docs/basic-usage/using-tags.md +++ b/docs/basic-usage/using-tags.md @@ -12,7 +12,7 @@ use Spatie\Tags\HasTags; class YourModel extends Model { use HasTags; - + ... } ``` @@ -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 diff --git a/src/HasTags.php b/src/HasTags.php index b469485..cb7d68e 100644 --- a/src/HasTags.php +++ b/src/HasTags.php @@ -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) @@ -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 diff --git a/src/Tag.php b/src/Tag.php index bab1079..07ce70c 100644 --- a/src/Tag.php +++ b/src/Tag.php @@ -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) diff --git a/tests/HasTagsScopesTest.php b/tests/HasTagsScopesTest.php index 70b9d19..f611406 100644 --- a/tests/HasTagsScopesTest.php +++ b/tests/HasTagsScopesTest.php @@ -111,7 +111,7 @@ 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(); @@ -119,10 +119,30 @@ public function it_provides_as_scope_to_get_all_models_that_have_any_of_the_give } /** @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()); + } } diff --git a/tests/TagTest.php b/tests/TagTest.php index fe252ab..c14393d 100644 --- a/tests/TagTest.php +++ b/tests/TagTest.php @@ -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() {