fix bug with ordering on pgsql

This commit is contained in:
2025-11-02 15:46:50 +05:00
parent 8078242cf9
commit 6f1de4531e
2 changed files with 29 additions and 16 deletions

View File

@@ -4,8 +4,10 @@ namespace App\Providers;
use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
use Spatie\Translatable\HasTranslations;
class AppServiceProvider extends ServiceProvider
{
@@ -30,5 +32,28 @@ class AppServiceProvider extends ServiceProvider
->localesLabels(baseLocales())
->locales(array_keys(baseLocales()));
});
/**
* Order by translation for spatie/laravel-translatable
*
* @param string $field
* @param string $order
* @param string $locale
* @return \Illuminate\Database\Eloquent\Builder
*/
Builder::macro('orderByTranslation', function (string $field, string $order = 'asc', ?string $locale = null) {
if (
in_array(HasTranslations::class, class_uses($this->model))
&& in_array($field, $this->model->translatable)
&& config('database.default') === 'pgsql'
) {
$locale = $locale ?? app()->getLocale();
$this->query->orderByRaw("$field->>'$locale' $order");
} else {
$this->query->orderBy($field, $order);
}
return $this;
});
}
}