Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

scopeWhereTranslationLike #183

Merged
merged 5 commits into from
Jan 9, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ Country::listsTranslations('name')->get()->toArray();

// Filters countries by checking the translation against the given value
Country::whereTranslation('name', 'Greece')->first();

// Filters countries by checking the translation against the given value
Country::whereTranslationLike('name', '%Gree%')->first();
```

### Magic properties
Expand Down
21 changes: 21 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,27 @@ public function scopeWhereTranslation($query, $key, $value, $locale = null)
}


/**
* This scope filters results by checking the translation fields.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $key
* @param string $value
* @param string $locale
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function scopeWhereTranslationLike($query, $key, $value, $locale = null)
{
return $query->whereHas('translations', function ($query) use ($key, $value, $locale) {
$query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value);
if ($locale) {
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale);
}
});
}


/**
* @return array
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/ScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,23 @@ public function test_whereTranslation_filters_by_translation_and_locale()
$this->assertSame(1, $result->count());
$this->assertSame('gr', $result->first()->code);
}

public function test_whereTranslationLike_filters_by_translation()
{
/** @var Country $country */
$country = Country::whereTranslationLike('name', '%Greec%')->first();
$this->assertSame('gr', $country->code);
}

public function test_whereTranslationLike_filters_by_translation_and_locale()
{
Country::create(['code' => 'some-code', 'name' => 'Griechenland']);

/** @var Country $country */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy paste? :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep :) just an extra scope

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this line:

/** @var Country $country */

$this->assertSame(2, Country::whereTranslationLike('name', 'Griechen%')->count());

$result = Country::whereTranslationLike('name', '%riechenlan%', 'de')->get();
$this->assertSame(1, $result->count());
$this->assertSame('gr', $result->first()->code);
}
}