fix bug with ordering on pgsql
This commit is contained in:
@@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user