Skip to content

Commit

Permalink
[FEAT] add ability for filtering a column's locale or multiple locale…
Browse files Browse the repository at this point in the history
…s with a specific value
  • Loading branch information
AbdelrahmanBl authored and freekmurze committed May 13, 2024
1 parent 4d409b6 commit e7e223c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ $newsItem->name; // Returns 'Naam in het Nederlands'
NewsItem::whereLocale('name', 'en')->get(); // Returns all news items with a name in English

NewsItem::whereLocales('name', ['en', 'nl'])->get(); // Returns all news items with a name in English or Dutch

// Returns all news items that has name in English with value `Name in English`
NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in English')->get();

// Returns all news items that has name in English or Dutch with value `Name in English`
NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in English')->get();


```

## Support us
Expand Down
10 changes: 10 additions & 0 deletions docs/basic-usage/querying-translations.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ NewsItem::whereLocale('name', 'en')->get(); // Returns all news items with a nam

NewsItem::whereLocales('name', ['en', 'nl'])->get(); // Returns all news items with a name in English or Dutch
```

If you want to query records based on locale's value, you can use the `whereJsonContainsLocale` and `whereJsonContainsLocales` methods.

```php
// Returns all news items that has name in English with value `Name in English`
NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in English')->get();

// Returns all news items that has name in English or Dutch with value `Name in English`
NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in English')->get();
```
14 changes: 14 additions & 0 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,20 @@ public function scopeWhereLocales(Builder $query, string $column, array $locales
});
}

public function scopeWhereJsonContainsLocale(Builder $query, string $column, string $locale, mixed $value): void
{
$query->whereJsonContains("{$column}->{$locale}", $value);
}

public function scopeWhereJsonContainsLocales(Builder $query, string $column, array $locales, mixed $value): void
{
$query->where(function (Builder $query) use ($column, $locales, $value) {
foreach($locales as $locale) {
$query->orWhereJsonContains("{$column}->{$locale}", $value);
}
});
}

/**
* @deprecated
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,28 @@ public function setAttributesExternally(array $attributes)
expect($this->testModel->whereLocales('name', ['de', 'be'])->get())->toHaveCount(0);
});

it('queries the database whether a value exists in a locale', function () {
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->setTranslation('name', 'fr', 'testValue_fr');
$this->testModel->setTranslation('name', 'tr', 'testValue_tr');
$this->testModel->save();

expect($this->testModel->whereJsonContainsLocale('name', 'en', 'testValue_en')->get())->toHaveCount(1);

expect($this->testModel->whereJsonContainsLocale('name', 'en', 'testValue_fr')->get())->toHaveCount(0);
});

it('queries the database whether a value exists in a multiple locales', function () {
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->setTranslation('name', 'fr', 'testValue_fr');
$this->testModel->setTranslation('name', 'tr', 'testValue_tr');
$this->testModel->save();

expect($this->testModel->whereJsonContainsLocales('name', ['en', 'fr'], 'testValue_en')->get())->toHaveCount(1);

expect($this->testModel->whereJsonContainsLocales('name', ['en', 'fr'], 'testValue_tr')->get())->toHaveCount(0);
});

it('can disable attribute locale fallback on a per model basis', function () {
config()->set('app.fallback_locale', 'en');

Expand Down

0 comments on commit e7e223c

Please sign in to comment.