60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
// ...
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
Model::unguard();
|
|
Model::shouldBeStrict(! app()->isProduction());
|
|
|
|
TranslatableTabs::configureUsing(function (TranslatableTabs $component) {
|
|
$component
|
|
->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;
|
|
});
|
|
}
|
|
}
|