-
-
Notifications
You must be signed in to change notification settings - Fork 170
Open
Description
When trying to use orderByTranslation() it eager loads every field from every translation.
Found here:
->with('translations') |
Ideally, we'd have the option to limit it to the current locale.
Is this possible?
As a workaround, I'm having to do something like this:
/**
* Apply sorting for translatable fields
*/
protected static function applyTranslatableSort(QueryBuilder $query, string $sortField, bool $isDesc): void
{
$direction = $isDesc ? 'desc' : 'asc';
$locale = getCurrentLocale();
$query->getEloquentBuilder()
->select('blog_categories.*')
->join('blog_category_translations AS sort_trans', function ($join) use ($locale) {
$join->on('blog_categories.id', '=', 'sort_trans.blog_category_id')
->where('sort_trans.locale', '=', $locale);
})
->orderBy("sort_trans.{$sortField}", $direction);
}