fix bug with ordering on pgsql
This commit is contained in:
@@ -61,13 +61,13 @@ class CardOrderForm
|
|||||||
Fieldset::make(__('Loan type and amount'))
|
Fieldset::make(__('Loan type and amount'))
|
||||||
->schema([
|
->schema([
|
||||||
Select::make('card_state_id')
|
Select::make('card_state_id')
|
||||||
->relationship('cardState', 'name', fn (Builder $query) => $query)
|
->relationship('cardState', 'name', fn (Builder $query) => $query->orderByTranslation('name'))
|
||||||
->label(__('Card state'))
|
->label(__('Card state'))
|
||||||
->native(false)
|
->native(false)
|
||||||
->required(),
|
->required(),
|
||||||
|
|
||||||
Select::make('card_type_id')
|
Select::make('card_type_id')
|
||||||
->relationship('cardType', 'name')
|
->relationship('cardType', 'name', fn (Builder $query) => $query->orderByTranslation('name'))
|
||||||
->label(__('Card type'))
|
->label(__('Card type'))
|
||||||
->native(false)
|
->native(false)
|
||||||
->required(),
|
->required(),
|
||||||
@@ -85,6 +85,8 @@ class CardOrderForm
|
|||||||
Select::make('branch_id')
|
Select::make('branch_id')
|
||||||
->label(__('Branch'))
|
->label(__('Branch'))
|
||||||
->relationship('branch', 'name', function ($query, callable $get) {
|
->relationship('branch', 'name', function ($query, callable $get) {
|
||||||
|
$query->orderByTranslation('name');
|
||||||
|
|
||||||
$region = $get('region');
|
$region = $get('region');
|
||||||
if ($region) {
|
if ($region) {
|
||||||
$query->where('region', $region);
|
$query->where('region', $region);
|
||||||
@@ -249,20 +251,6 @@ class CardOrderForm
|
|||||||
->columnSpan(2),
|
->columnSpan(2),
|
||||||
])->columnSpan(4),
|
])->columnSpan(4),
|
||||||
])->columnSpanFull(),
|
])->columnSpanFull(),
|
||||||
|
|
||||||
// Textarea::make('passport_one')
|
|
||||||
// ->required()
|
|
||||||
// ->columnSpanFull(),
|
|
||||||
// Textarea::make('passport_two')
|
|
||||||
// ->required()
|
|
||||||
// ->columnSpanFull(),
|
|
||||||
// Textarea::make('passport_three')
|
|
||||||
// ->required()
|
|
||||||
// ->columnSpanFull(),
|
|
||||||
// Textarea::make('passport_four')
|
|
||||||
// ->required()
|
|
||||||
// ->columnSpanFull(),
|
|
||||||
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ namespace App\Providers;
|
|||||||
|
|
||||||
use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
|
use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Spatie\Translatable\HasTranslations;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
@@ -30,5 +32,28 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
->localesLabels(baseLocales())
|
->localesLabels(baseLocales())
|
||||||
->locales(array_keys(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