Skip to content

Commit d723f33

Browse files
authored
Merge pull request #72 from Lloople/order_by_locale
Get all translations when ordering by locale
2 parents 34f11e9 + 7de725c commit d723f33

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/Translatable/Traits/Scopes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public function scopeOrderByTranslation(Builder $query, string $translationField
5656
$keyName = $this->getKeyName();
5757

5858
return $query
59-
->join($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) {
59+
->with('translations')
60+
->select("{$table}.*")
61+
->leftJoin($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) {
6062
$join
61-
->on($translationTable.'.'.$this->getTranslationRelationKey(), '=', $table.'.'.$keyName)
62-
->where($translationTable.'.'.$localeKey, $this->locale());
63+
->on("{$translationTable}.{$this->getTranslationRelationKey()}", '=', "{$table}.{$keyName}")
64+
->where("{$translationTable}.{$localeKey}", $this->locale());
6365
})
64-
->orderBy($translationTable.'.'.$translationField, $sortMethod)
65-
->select($table.'.*')
66-
->with('translations');
66+
->orderBy("{$translationTable}.{$translationField}", $sortMethod);
6767
}
6868

6969
public function scopeOrWhereTranslation(Builder $query, string $translationField, $value, ?string $locale = null)

tests/ScopesTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Astrotomic\Translatable\Test\Model\Food;
34
use Astrotomic\Translatable\Test\Model\Country;
45
use Astrotomic\Translatable\Test\Model\Vegetable;
56

@@ -203,12 +204,26 @@ public function test_whereTranslationLike_filters_by_translation_and_locale()
203204
public function test_orderByTranslation_sorts_by_key_asc()
204205
{
205206
$result = Country::orderByTranslation('name')->get();
206-
$this->assertSame(2, $result->first()->id);
207+
$this->assertSame(3, $result->first()->id);
207208
}
208209

209210
public function test_orderByTranslation_sorts_by_key_desc()
210211
{
211212
$result = Country::orderByTranslation('name', 'desc')->get();
212213
$this->assertSame(1, $result->first()->id);
213214
}
215+
216+
public function test_orderByTranslation_sorts_by_key_asc_even_if_locale_is_missing()
217+
{
218+
Food::create(['en' => ['name' => 'Potatoes'], 'fr' => ['name' => 'Pommes de Terre']]);
219+
Food::create(['en' => ['name' => 'Strawberries'], 'fr' => ['name' => 'Fraises']]);
220+
Food::create([]);
221+
222+
$orderInEnglish = Food::with('translations')->orderByTranslation('name')->get();
223+
$this->assertEquals([null, 'Potatoes', 'Strawberries'], $orderInEnglish->pluck('name')->toArray());
224+
225+
App::setLocale('fr');
226+
$orderInFrench = Food::with('translations')->orderByTranslation('name', 'desc')->get();
227+
$this->assertEquals(['Pommes de Terre', 'Fraises', null], $orderInFrench->pluck('name')->toArray());
228+
}
214229
}

0 commit comments

Comments
 (0)