Skip to content

Commit

Permalink
Merge pull request #72 from Lloople/order_by_locale
Browse files Browse the repository at this point in the history
Get all translations when ordering by locale
  • Loading branch information
Gummibeer authored Oct 9, 2019
2 parents 34f11e9 + 7de725c commit d723f33
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/Translatable/Traits/Scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public function scopeOrderByTranslation(Builder $query, string $translationField
$keyName = $this->getKeyName();

return $query
->join($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) {
->with('translations')
->select("{$table}.*")
->leftJoin($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) {
$join
->on($translationTable.'.'.$this->getTranslationRelationKey(), '=', $table.'.'.$keyName)
->where($translationTable.'.'.$localeKey, $this->locale());
->on("{$translationTable}.{$this->getTranslationRelationKey()}", '=', "{$table}.{$keyName}")
->where("{$translationTable}.{$localeKey}", $this->locale());
})
->orderBy($translationTable.'.'.$translationField, $sortMethod)
->select($table.'.*')
->with('translations');
->orderBy("{$translationTable}.{$translationField}", $sortMethod);
}

public function scopeOrWhereTranslation(Builder $query, string $translationField, $value, ?string $locale = null)
Expand Down
17 changes: 16 additions & 1 deletion tests/ScopesTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Astrotomic\Translatable\Test\Model\Food;
use Astrotomic\Translatable\Test\Model\Country;
use Astrotomic\Translatable\Test\Model\Vegetable;

Expand Down Expand Up @@ -203,12 +204,26 @@ public function test_whereTranslationLike_filters_by_translation_and_locale()
public function test_orderByTranslation_sorts_by_key_asc()
{
$result = Country::orderByTranslation('name')->get();
$this->assertSame(2, $result->first()->id);
$this->assertSame(3, $result->first()->id);
}

public function test_orderByTranslation_sorts_by_key_desc()
{
$result = Country::orderByTranslation('name', 'desc')->get();
$this->assertSame(1, $result->first()->id);
}

public function test_orderByTranslation_sorts_by_key_asc_even_if_locale_is_missing()
{
Food::create(['en' => ['name' => 'Potatoes'], 'fr' => ['name' => 'Pommes de Terre']]);
Food::create(['en' => ['name' => 'Strawberries'], 'fr' => ['name' => 'Fraises']]);
Food::create([]);

$orderInEnglish = Food::with('translations')->orderByTranslation('name')->get();
$this->assertEquals([null, 'Potatoes', 'Strawberries'], $orderInEnglish->pluck('name')->toArray());

App::setLocale('fr');
$orderInFrench = Food::with('translations')->orderByTranslation('name', 'desc')->get();
$this->assertEquals(['Pommes de Terre', 'Fraises', null], $orderInFrench->pluck('name')->toArray());
}
}

0 comments on commit d723f33

Please sign in to comment.