diff --git a/README.md b/README.md index db0f0ca..f532ba6 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,11 @@ NewsItem::withAllTags(['first tag', 'second tag'])->get(); // retrieve models that don't have any of the given tags NewsItem::withoutTags(['first tag', 'second tag'])->get(); +// force tag locale (useful when you have a multi-language app but don't want to translate tags) +NewsItem::withAnyTags(['first tag', 'second tag'], locale: 'en')->get(); +NewsItem::withAllTags(['first tag', 'second tag'], locale: 'en')->get(); +NewsItem::withoutTags(['first tag', 'second tag'], locale: 'en')->get(); + // translating a tag $tag = Tag::findOrCreate('my tag'); $tag->setTranslation('name', 'fr', 'mon tag'); diff --git a/src/HasTags.php b/src/HasTags.php index 959b1b0..0ffe2ce 100644 --- a/src/HasTags.php +++ b/src/HasTags.php @@ -90,8 +90,9 @@ public function scopeWithAllTags( Builder $query, string | array | ArrayAccess | Tag $tags, string $type = null, + string $locale = null ): Builder { - $tags = static::convertToTags($tags, $type); + $tags = static::convertToTags($tags, $type, $locale); collect($tags)->each(function ($tag) use ($query) { $query->whereHas('tags', function (Builder $query) use ($tag) { @@ -106,8 +107,9 @@ public function scopeWithAnyTags( Builder $query, string | array | ArrayAccess | Tag $tags, string $type = null, + string $locale = null ): Builder { - $tags = static::convertToTags($tags, $type); + $tags = static::convertToTags($tags, $type, $locale); return $query ->whereHas('tags', function (Builder $query) use ($tags) { @@ -120,9 +122,10 @@ public function scopeWithAnyTags( public function scopeWithoutTags( Builder $query, string | array | ArrayAccess | Tag $tags, - string $type = null + string $type = null, + string $locale = null ): Builder { - $tags = static::convertToTags($tags, $type); + $tags = static::convertToTags($tags, $type, $locale); return $query ->whereDoesntHave('tags', function (Builder $query) use ($tags) { @@ -132,9 +135,12 @@ public function scopeWithoutTags( }); } - public function scopeWithAllTagsOfAnyType(Builder $query, $tags): Builder - { - $tags = static::convertToTagsOfAnyType($tags); + public function scopeWithAllTagsOfAnyType( + Builder $query, + $tags, + string $locale = null + ): Builder { + $tags = static::convertToTagsOfAnyType($tags, $locale); collect($tags) ->each(function ($tag) use ($query) { @@ -147,9 +153,12 @@ public function scopeWithAllTagsOfAnyType(Builder $query, $tags): Builder return $query; } - public function scopeWithAnyTagsOfAnyType(Builder $query, $tags): Builder - { - $tags = static::convertToTagsOfAnyType($tags); + public function scopeWithAnyTagsOfAnyType( + Builder $query, + $tags, + string $locale = null + ): Builder { + $tags = static::convertToTagsOfAnyType($tags, $locale); $tagIds = collect($tags)->pluck('id'); diff --git a/tests/HasTagsTranslatedTest.php b/tests/HasTagsTranslatedTest.php index dfbb029..e8ab262 100644 --- a/tests/HasTagsTranslatedTest.php +++ b/tests/HasTagsTranslatedTest.php @@ -31,3 +31,47 @@ expect($translated['name'][$locale])->toEqual($translated['name_translated']); expect($translated['slug'][$locale])->toEqual($translated['slug_translated']); }); + +it('retrieves English tags with tagsTranslated method regardless of application locale', function () { + $testModel = TestModel::create(['name' => 'default']); + $testModel->attachTag('Test Tag'); + + app()->setLocale('fr'); + + $tags = $testModel->tagsTranslated('en')->get(); + expect($tags->first()->name_translated)->toEqual('Test Tag'); +}); + +it('filters models with any English tags regardless of application locale', function () { + $testModel = TestModel::create(['name' => 'default']); + $testModel->attachTag('Test Tag'); + + app()->setLocale('fr'); + + $modelsWithEnglishTag = TestModel::withAnyTags('Test Tag', null, 'en')->get(); + expect($modelsWithEnglishTag)->toHaveCount(1); + expect($modelsWithEnglishTag->first()->name)->toEqual('default'); +}); + +it('filters models with all specified English tags regardless of application locale', function () { + $testModel = TestModel::create(['name' => 'default']); + $testModel->attachTag('Test Tag'); + $testModel->attachTag('Another Tag'); + + app()->setLocale('fr'); + + $modelsWithAllTags = TestModel::withAllTags(['Test Tag', 'Another Tag'], null, 'en')->get(); + expect($modelsWithAllTags)->toHaveCount(1); + expect($modelsWithAllTags->first()->name)->toEqual('default'); +}); + +it('excludes models with specified English tags regardless of application locale', function () { + TestModel::create(['name' => 'other'])->attachTag('Different Tag'); + $testModel = TestModel::create(['name' => 'default']); + $testModel->attachTag('Test Tag'); + + app()->setLocale('fr'); + + $modelsWithoutTag = TestModel::withoutTags('Test Tag', null, 'en')->get(); + expect($modelsWithoutTag)->each->not->toHaveProperty('name', 'default'); +});