Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added operand for json scopes #454

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ 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

// Returns all news items that has name in English with value `Name in English`
NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in English')->get();
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();
NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in English')->get();

// The last argument is the "operand" which you can tweak to achieve something like this:

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

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


```
Expand Down
10 changes: 5 additions & 5 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,16 @@ public function scopeWhereLocales(Builder $query, string $column, array $locales
});
}

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

public function scopeWhereJsonContainsLocales(Builder $query, string $column, array $locales, mixed $value): void
public function scopeWhereJsonContainsLocales(Builder $query, string $column, array $locales, mixed $value, string $operand = '='): void
{
$query->where(function (Builder $query) use ($column, $locales, $value) {
$query->where(function (Builder $query) use ($column, $locales, $value, $operand) {
foreach($locales as $locale) {
$query->orWhere("{$column}->{$locale}", $value);
$query->orWhere("{$column}->{$locale}", $operand, $value);
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,8 @@ public function setAttributesExternally(array $attributes)

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

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

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

Expand All @@ -788,6 +790,8 @@ public function setAttributesExternally(array $attributes)

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

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

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

Expand Down
Loading