Skip to content

Commit 78997ee

Browse files
authored
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
1 parent 7b7e360 commit 78997ee

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

docs/basic-usage/using-tags.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use Spatie\Tags\HasTags;
1212
class YourModel extends Model
1313
{
1414
use HasTags;
15-
15+
1616
...
1717
}
1818
```
@@ -70,13 +70,16 @@ $tag->name = 'another tag';
7070
$tag->save();
7171

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

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

7878
//delete a tag
7979
$tag->delete();
80+
81+
//use "findFromStringOfAnyType" to retrieve a collection of tags with various types
82+
$tags = Tag::findFromStringOfAnyType('one more tag');
8083
```
8184

8285
## Finding tags

src/HasTags.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public function scopeWithAnyTagsOfAnyType(Builder $query, $tags): Builder
119119

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

122-
123122
return $query->whereHas(
124123
'tags',
125124
fn (Builder $query) => $query->whereIn('tags.id', $tagIds)
@@ -216,7 +215,7 @@ protected static function convertToTagsOfAnyType($values, $locale = null)
216215
$className = static::getTagClassName();
217216

218217
return $className::findFromStringOfAnyType($value, $locale);
219-
});
218+
})->flatten();
220219
}
221220

222221
protected function syncTagIds($ids, string | null $type = null, $detaching = true): void

src/Tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function findFromStringOfAnyType(string $name, string $locale = nu
8181

8282
return static::query()
8383
->where("name->{$locale}", $name)
84-
->first();
84+
->get();
8585
}
8686

8787
protected static function findOrCreateFromString(string $name, string $type = null, string $locale = null)

tests/HasTagsScopesTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,38 @@ public function it_provides_as_scope_to_get_all_models_that_have_all_of_the_give
111111
}
112112

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

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

121121
/** @test */
122-
public function it_provides_as_scope_to_get_all_models_that_have_all_of_the_given_tags_with_any_type()
122+
public function it_provides_a_scope_to_get_all_models_that_have_any_of_the_given_tags_with_any_type_from_mixed_tag_values()
123+
{
124+
$tagD = Tag::findFromString('tagD');
125+
126+
$testModels = TestModel::withAnyTagsOfAnyType([$tagD, 'tagE', 'tagF'])->get();
127+
128+
$this->assertEquals(['model4', 'model5', 'model6'], $testModels->pluck('name')->toArray());
129+
}
130+
131+
/** @test */
132+
public function it_provides_a_scope_to_get_all_models_that_have_all_of_the_given_tags_with_any_type()
123133
{
124134
$testModels = TestModel::withAllTagsOfAnyType(['tagE', 'tagF'])->get();
125135

126136
$this->assertEquals(['model5'], $testModels->pluck('name')->toArray());
127137
}
138+
139+
/** @test */
140+
public function it_provides_a_scope_to_get_all_models_that_have_all_of_the_given_tags_with_any_type_from_mixed_tag_values()
141+
{
142+
$tagE = Tag::findFromString('tagE', 'typedTag');
143+
144+
$testModels = TestModel::withAllTagsOfAnyType([$tagE, 'tagF'])->get();
145+
146+
$this->assertEquals(['model5'], $testModels->pluck('name')->toArray());
147+
}
128148
}

tests/TagTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ public function it_can_find_or_create_a_tag()
173173
$this->assertEquals('string', $tag2->name);
174174
}
175175

176+
/** @test */
177+
public function it_can_find_tags_from_a_string_with_any_type()
178+
{
179+
Tag::findOrCreate('tag1');
180+
181+
Tag::findOrCreate('tag1', 'myType1');
182+
183+
Tag::findOrCreate('tag1', 'myType2');
184+
185+
$tags = Tag::findFromStringOfAnyType('tag1');
186+
187+
$this->assertCount(3, $tags);
188+
}
189+
176190
/** @test */
177191
public function its_name_can_be_changed_by_setting_its_name_property_to_a_new_value()
178192
{

0 commit comments

Comments
 (0)